• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList of strings vs string array - speed and memory

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic