0

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.

Should I declare the 'Class' class as

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<>(); 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

eArshdeep
  • 29
  • 5
  • Carrots? xaxaxa epic!.You mean diamond operator . Have a look at: http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7#4167148 – GOXR3PLUS Nov 12 '16 at 03:05
  • I remember watching a video a long time ago and someone referring to it as carrots and adding that it was controversial or something. I didn't care to look up the correct term now as I was occupied with this. Anyway, I understand the fool this makes me look :} – eArshdeep Nov 12 '16 at 03:13
  • I don't meant that.It just was so funny :) – GOXR3PLUS Nov 12 '16 at 03:14
  • So, in "List list = new LinkedList<>();" what is LinkedList? How can you have something like that? – eArshdeep Nov 12 '16 at 03:20
  • And in "ArrayList myStudents = new ArrayList();" is this an object of class ArrayList or class 'Class' – eArshdeep Nov 12 '16 at 03:20

2 Answers2

0

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>().

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. It helps to write the code in a cleaner way. For e.g.

Writing below is easier:

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

than:

List<Some<Type<Another>>> var = new ArrayList<Some<Type<Another>>>();
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

Technically, neither.

You would want to do:

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

if you want to create an ArrayList with Student objects and

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

if you want to create an ArrayList with Class objects.

1) Note the variable names.

2) Note that you should always try to code to an interface (the left side is a List, not an ArrayList). This allows much greater flexibility since you're not dependent on the specific implementation of an ArrayList later on. 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. Depending on how you are using your ArrayList later, the Collection interface may be sufficient instead.

The diamond operator allows the compiler to infer the value of the type argument without having to type it all out. It's needed for backward compatibility for older Java versions.

As a general practice for performance optimization, you will also want to supply an initial capacity of an ArrayList if it's possible. So if you know that there are only 5 classes, then you would do:

List<Class> myClasses = new ArrayList<>(5);
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80