指数一个ArrayList的ID号 [英] Index the ID number in an ArrayList

查看:131
本文介绍了指数一个ArrayList的ID号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个从.txt文件取一个ArrayList。它显示以下细节:

So, I have an ArrayList that is taken from a .txt file. It displays the following details:

Id
Car Manufacturer
Car Type
Colour
*blank line*

例如:

21
Vauxhall
Corsa
red

19
Vauxhall
Corsa
blue

18
Vauxhall
Corsa
White

我很好奇,如何使该ID的一个ArrayList的指数,这样,如果汽车被删除或添加到ArrayList,它会自动调整ID号。

I am curious as how to make the Id the "index" of an ArrayList, so that if a car was deleted or added to the Arraylist, It would automatically adjust the Id number.

推荐答案

您使用错误的数据结构...你的目的是这样的HashMap或者LinkedHashMap会更理想。你可以把你所有的汽车数据的POJO

You using wrong DataStructure... For your purpose something like HashMap or LinkedHashMap would be more optimal. You can put all your car data in pojos

class Car {
int Id;
String make, style, color; 
getters/setters....; 
}
List<Car> list = myReadMethod();

//Then you make 
Map<int, Car> myCars = new HashMap<Int, Car>;
for(Car car : list)
     myCars.put(car.getId, car);  

这篇关于指数一个ArrayList的ID号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆