简体   繁体   English

ArrayList使用什么类?

[英]What class to use for ArrayList?

I just wanted to clarify this question I had for a while for more efficient and 'correct' code. 我只是想澄清一下这个问题,以获得更高效和“正确”的代码。

I gave a class 'Student' with objects in an array list of objects. 我在对象数组列表中为对象提供了“学生”类。 I have another class called Class which has an array list of references to the very same objects in the Student class. 我有另一个名为Class的类,该类具有对Student类中相同对象的引用的数组列表。

Should I declare the 'Class' class as 我应该将“ Class”类声明为

ArrayList<Student> myStudents = new ArrayList<Student>();

or 要么

ArrayList<Class> myStudents = new ArrayList<Class>();

Also another part of the question is I have seen people declare arrayLists as ArrayList<Student> myStudents = new ArrayList<>(); 问题的另一部分是,我看到人们将arrayLists声明为ArrayList<Student> myStudents = new ArrayList<>(); where the second half of the carrots are left empty. 胡萝卜的后半部分留空。 What exactly does the difference mean? 区别到底是什么意思? Does this mean that the array list is not an object of any class? 这是否意味着数组列表不是任何类的对象?

Thank you so much for your time and help Cheers 非常感谢您抽出宝贵的时间为您提供帮助

It depends on what you want to store in the list rather than where you are using it. 这取决于要在列表中存储的内容,而不是要在哪里使用它。 If you're storing Student objects, then you'll use ArrayList<Student>() . 如果要存储Student对象,则将使用ArrayList<Student>()

The type omitted on the right side is called type inference (added in java 7), which means the type parameter on the right side will be inferred from the type of the assignment variable on the left. 右侧省略的类型称为类型推断(在Java 7中已添加),这意味着将从左侧的赋值变量的类型推断出右侧的type参数。 It helps to write the code in a cleaner way. 这有助于以更简洁的方式编写代码。 For eg 例如

Writing below is easier: 下面的编写更加容易:

List<Some<Type<Another>>> var = new ArrayList<>();

than: 比:

List<Some<Type<Another>>> var = new ArrayList<Some<Type<Another>>>();

Technically, neither . 从技术上讲, 两者都没有

You would want to do: 您想做:

List<Student> myStudents = new ArrayList<>();

if you want to create an ArrayList with Student objects and 如果您想使用Student对象创建一个ArrayList

List<Class> myClasses = new ArrayList<>();

if you want to create an ArrayList with Class objects. 如果要使用Class对象创建ArrayList

1) Note the variable names. 1)注意变量名。

2) Note that you should always try to code to an interface (the left side is a List , not an ArrayList ). 2)请注意,您应该始终尝试对接口进行编码(左侧是List ,而不是ArrayList )。 This allows much greater flexibility since you're not dependent on the specific implementation of an ArrayList later on. 由于您以后不再依赖ArrayList的特定实现,因此可以提供更大的灵活性。 This point is so powerful! 这点是如此强大! You can write method signatures to accept objects of type List and then use an ArrayList , LinkedList or Stack or any class that implements a List . 您可以编写方法签名来接受List类型的对象,然后使用ArrayListLinkedListStack或实现List任何类。 Depending on how you are using your ArrayList later, the Collection interface may be sufficient instead. 根据以后使用ArrayListCollection接口可能就足够了。

The diamond operator allows the compiler to infer the value of the type argument without having to type it all out. Diamond运算符允许编译器推断出type参数的值,而不必全部输入。 It's needed for backward compatibility for older Java versions. 它是旧版本Java向后兼容所必需的。

As a general practice for performance optimization, you will also want to supply an initial capacity of an ArrayList if it's possible. 作为性能优化的一般做法,如果可能的话,您还将希望提供ArrayList的初始容量。 So if you know that there are only 5 classes, then you would do: 因此,如果您知道只有5个类,则可以这样做:

List<Class> myClasses = new ArrayList<>(5);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM