• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList vs String[]  RSS feed

 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it matter which class you use if you want to loop through a collection of Strings? Does one have better performance? For example:

ArrayList list = new ArrayList(4);
list.add( "One" );
list.add( "Two" );
list.add( "Three" );
list.add( "Four" );

String[] list = { "One", "Two", "Three", "Four" };

Using either one of these to loop with, which one is more efficient or does it matter?

Thanks,
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, performance differs, and you needn't be advanced, to make a measurement yourself.

Performance depends on what operations you need to perform.
Inserts, reordering, find, removing, iterating, sorting, appending ...
Each kind of collection has its strengths.

If the size is modified during runtime, array is simply not suitable.

Often performance-differences aren't measurable for small amounts of data - let's say 10000 strings - but that may vary on your usage, the hardware, the JVM, ...
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan for your response. I posted this question in the advanced forum to try and get only experienced programmers answers. Like I mentioned in my first post, if I have a small collection of String that I want to iterate through, which is more efficient, better performance and good programming practice? Use an ArrayList or String[]? Personally, I prefer to use the ArrayList only because I'm not fond arrays.

Thank you.
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stefan had said, it's totally depend on what you are using it for.
If you'll have a lot of insertion and deletion, ArrayList will be faster.
If you have fixed size and only use it for iteration, simple array will be faster.

And again, most of the time the difference is not worth the trouble to fret over it. Just go with what you like, in this case ArrayList.

If you are posting on advanced forum and want expert opinion, than you better be more specific on what is your need and trying to achieve, e.g.: how to implement Goggle Desktop like indexing scheme or something like that.
 
Shannon Sims
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wirianto for your reply. I realize that perhaps this wasn't the right forum to post this question, but I cannot elaborate any further. Let me just say, that I will use your response to fend for my code. =) Some people are VERY nit picky.

Thank you.
 
Garrett OBrien
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that Arrays are really that much faster than ArrayLists. If you look at the implementation of ArrayList, the underlying data is an array, so the only speed difference is a few minor method calls vs. direct access to the array, and if you size the ArrayList at the beginning, adding to the ArrayList won't really take any more time either, if you go over the initial capacity of the array, then it will need to create a new internal array and copy the first array values to the second array.

Just my 2 cents.

Garrett
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!