• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the best way to set up an arraylist?  RSS feed

 
Arnold Pitz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

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?

Thanks in advance for the feedback guys.

source
Screen-Shot-2016-01-25-at-3.32.41-PM.png
[Thumbnail for Screen-Shot-2016-01-25-at-3.32.41-PM.png]
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several comments I could make but it's hard to work with a screen snapshot. Could you please cut-and-past the code directly into the post, and don't forget to use 'code' tags.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a Collection type you should always specify the type of the Collection.

List<Integer> myList = new ArrayList<Integer>();
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

For Naming Convention click here
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These ways you can initialize ArrayList object:




 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:These ways you can initialize ArrayList object: . . .
But five of those eight ways to do it are wrong (or at least not ideal); please tell us which are right (or wrong) and why.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ?
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I'll use thread safe only when required. as usual you always helps me so For cow also
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!