This is what I posted as an example 2. Is this the best way to setup an array list or is there a better way you guys would reccomend? Please take a look at the photo and let me know what you think I could improve on?
First of all Welcome to CodeRanch! Always post codes using code tags please read this. To know all tags peruse this also link.
Always post real code not an image so will be easy to help you and solve your problems.
I made changes in names of class and methods and wrote separate methods so what program does will be easy to understand.Next time you have to write code in this way
1, 2, 3, 4 and 7 I feel not ideal.
5, 6 and 8 are ideal.
Why? for not ideal
In 1, 4 and 7 I didn't use generic collection which was introduced in 1.5(Hope I'm correct), Here problem is, it can access any data type as element if I have ArrayList just for age i.e. only numbers should be allowed but here it can take String and other which may give error when retrieving information from that ArrayList and storing that in int or doing some math operation on age as I would think user will enter or ArrayList will have numbers only. So to correct this use generic collection means specify what types of elements i.e. String, int etc that ArayList should allow to store
It is always better to use reference variable of interface because assume that If I use LinkedList then later found that I needed only retrieving of information then realized I should have used ArrayList which way better in accessing information than LinkedList as ArrayList implements RandomAccess a Marker interface used by List implementations to indicate that they support fast (generally constant time) random access then that time I just have to change code on one single line like List myList = new LinkedList(); make it List myList = new ArrayList()
I should synchronize them as we know ArrayList is not synchronized means only one thread should have access to that ArrayList at a time
I really didn't know about 2nd point I explained( just googled and found answer else wouldn't have posted above in that ways) except I knew about ArrayList implements RandomAccess Marker interface and is faster example If I have millions of elements in it then .get(index) methods gets me that directly accessing it but in other which doesn't implement RandomAccess will traverse through all then retrieves required element so this takes time.
Why? for ideal 5, 6 and 8 are where I specified what sort of elements it can hold, here int but still if we consider thread safety then 5 and 6 are yet not ideal because they are not synchronized.
But if I use List myList = new LinkedList() then as I've given answer why above are not ideal which doesn't uses reference variable of interface in situation where access is important but what about that where I used myList.offer() method which is the method of LinkedList class only. This method is neither in List nor in ArrayList. do I have to change code where I used this method to add(E e) method of ArrayList where both of the methods adds elements at last ?
Being Java programmer.
posted 3 years ago
I think you are correct. You should consider whether thread safety is required; it might not. So two of those declarations are good in a single threaded environment but not in a multi‑threaded environment.