This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Is this the best way to set up an arraylist?

 
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]
 
Saloon Keeper
Posts: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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: 6449
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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>();
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These ways you can initialize ArrayList object:




 
Marshal
Posts: 66469
250
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • 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: 66469
    250
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • 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
     
    Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!