wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ArrayList of strings vs string array - speed and memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList of strings vs string array - speed and memory" Watch "ArrayList of strings vs string array - speed and memory" New topic
Author

ArrayList of strings vs string array - speed and memory

justin smythhe
Ranch Hand

Joined: Jul 06, 2012
Posts: 107
Lets say I have to store X number of strings which is known at compile time. X could be in the range - 10-50 or even more.
Should I put them in an Array or in an ArrayList of strings ?
What is the difference in the memory consumed and the speed of access ?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

There will be not enough difference in either to make a demonstrable difference. So use which one makes the most sense and improves the clarity of the code.

Or, are there other criteria that you haven't mentioned; such as, insertions and re-sizing?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
justin smythhe
Ranch Hand

Joined: Jul 06, 2012
Posts: 107
Bear Bibeault wrote:There will be not enough difference in either to make a demonstrable difference. So use which one makes the most sense and improves the clarity of the code.

Or, are there other criteria that you haven't mentioned; such as, insertions and re-sizing?


I know that resizing and insertions will be much easier with arraylists. Are there any other criteria besides this ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It's your application, so it's up to you to say what you plan to do with this list of strings. So far you've only said you plan to store them. In any real-life application you would be doing other things beyond just storing the data.

But in real life, since the question is probably quite unimportant, the programmer would just pick something reasonable and carry on until it was proven to be a bad choice. At that time the programmer would refactor to use some better choice.
justin smythhe
Ranch Hand

Joined: Jul 06, 2012
Posts: 107
Paul Clapham wrote:It's your application, so it's up to you to say what you plan to do with this list of strings. So far you've only said you plan to store them. In any real-life application you would be doing other things beyond just storing the data.

But in real life, since the question is probably quite unimportant, the programmer would just pick something reasonable and carry on until it was proven to be a bad choice. At that time the programmer would refactor to use some better choice.


How does one find out if the choice was bad ? Maybe check the speed of execution and memory consumed ?
Stevens Miller
Ranch Hand

Joined: Jul 26, 2012
Posts: 523
    
    3

Paul Clapham wrote:But in real life, since the question is probably quite unimportant, the programmer would just pick something reasonable and carry on until it was proven to be a bad choice. At that time the programmer would refactor to use some better choice.


What Paul said. If you're nervous about your choice surviving for the long term, just make sure your accesses are all easily converted from one choice to the other. ArrayList and array objects are similar enough that you might not need to do much, but you could write wrappers for your get, insert, add, etc. operations, which would allow you easily to convert from one choice to another by changing a small amount of code in just a few places (that is, in each of your wrappers).

I predict, however, that no matter which choice you make, you'll never need to change it. There was a time when this sort of thing made a big difference in all kinds of applications. For some things, of course, it still does. Just not so many as it used to.

"The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet." -Michael A. Jackson
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

justin smythhe wrote:How does one find out if the choice was bad ? Maybe check the speed of execution and memory consumed ?


You aren't getting it. You've got a trivially small list of strings. No matter what you choose to store them in, it isn't going to make a noticeable difference to the running time or memory usage of your code. Performance is highly unlikely to be an issue here so it isn't worth thinking about it.

You find out the choice is bad when you find yourself having to write weird code to deal with your list of strings. For example let's suppose you decided to store them in an array. And then later you decide that you have to be able to add more strings to the list, after you've initialized it. That means that an array was a bad choice and you should have chosen some kind of collection which doesn't have a fixed size.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

justin smythhe wrote:How does one find out if the choice was bad ? Maybe check the speed of execution and memory consumed ?

You run the program for a while, and wait to hear from the users.

Generally, these issues aren't considered unless you have specific, real-time, well defined requirements. A missile guidance system has to have some pretty quick response time to queries, and these would be defined ahead of time.

If you are just writing an app for an end user, it is probably 99.99999% likely that nobody will ever notice the difference between the two. Things like disk access time or network traffic lag time will often be a MUCH bigger factor.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
jQuery in Action, 2nd edition
 
subject: ArrayList of strings vs string array - speed and memory
 
Similar Threads
Memory Of the Variable
Making arraylist from resultset
String memory usage
Array of Objects to be passed into a method
does String take a fixed size in memory?