Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array vs ArrayList

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am writing a card game that contains a number of arrays and arraylists that hold players cards, the deck, etc. What is the better option in terms of memory? Is there a golden rule like "if the length of the array never changes then use an array", etc?

Thanks
Alan
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the real golden rule is "don't worry about performance and memory unless you know for certain it's a problem". I wouldn't choose between Lists and arrays on that basis. I'd say: if the length changes then definitely use a List. If it won't, then you might as well use a List anyway most of the time.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general I'd go for a List over an array. The memory usage will be almost identical, except for extremely small Lists/arrays, in which case it won't matter. And the performance for any normal use case will be indistinguishable.

Pick the one that suits your design needs best, and don't worry about microoptimizing until and unless you measure this structure to be a bottleneck.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, ill stick with the ArrayList so. Thanks for the info!

Alan
 
Winston Gutkowski
Bartender
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:Ok, ill stick with the ArrayList so. Thanks for the info!

While I agree completely with what the other guys have said, an array of primitives (eg int[]) is likely to be quite a bit faster than an equivalent fixed-length List of wrappers (List<Integer>), particularly if it's changed often, as for example with an array of counters.

That said, if the latter is fast enough, I'd still use it over an array. In the above case, you could also use a List<AtomicInteger>.

Winston

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
That said, if the latter is fast enough


Which it usually will be, unless you're doing some LHC-type of calculations, in which case maybe you should have used FORTRAN instead.
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add that an array of primitives can be quite a bit more memory-efficient than a List of any sort. Particularly a byte[] is usually considerably smaller than a corresponding List<Byte>. Like the performance considerations, that doesn't matter much, most of the time. But if you're dealing with a large amount of data, the effect can be substantial.
 
Sumiran Pradhan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely use the List over arrays. If you do not exceed the capacity it is going to be as fast as an array. Better handling and much easier way of doing things.

Something like :

Convert to an array
 
Victor M. Pereira
Ranch Hand
Posts: 50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array is faster and that is because ArrayList uses a fixed amount of array. However when you add an element to the ArrayList and it overflows. It creates a new Array and copies every element from the old one to the new one. You will only feel this if you add to often.

Since the add from ArrayList is O(n) and the add to the Array is O(1).

However because ArrayList uses an Array is faster to search O(1) in it than normal lists O(n).



ArrayList Code
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumiran Pradhan wrote:Definitely use the List over arrays.


AGAIN...

It depends on the situation.

*sigh*
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Victor M. Pereira wrote:Array is faster and that is because


As already stated...

In the vast majority of cases, that won't matter in the least.

Since the add from ArrayList is O(n) and the add to the Array is O(1).


Wrong.

Addding to an ArrayList is O(1).
 
atinder singh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array are of fixed size Whereas an ArrayList implements the list data structure and can dynamically grow
you can find more about array on this link
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, you can't 'add' to an array. You can only 'set'.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:you can't 'add' to an array. You can only 'set'.

you can add; it takes O(n) in worst case and O(1) time in best case ;)
 
Mike Simmons
Ranch Hand
Posts: 3040
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Stephan van Hulst wrote:you can't 'add' to an array. You can only 'set'.

you can add; it make takes O(n) in worst case and O(1) time in best case ;)

I think you're thinking of an ArrayList, not an Array. For an ArrayList, O(1) is both the best case, and the average case - incidences of O(n) are rare enough (and more importantly, progressively rare) that they can be subsumed into the constant O(1).

An array has no methods, neither add() nor set(). However the basic operations possible with an array are equivalent to get() and set() from List. There is no operation equivalent to List's add(), which would increase the size of the List by 1. That simply can't happen with an array.

But this is all old news, isn't it?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I want to say is that ArrayList functions you can do with Array *explicitly* . add will not replace existing index element. so you need to shift all the elements one step towards end. and set will replace an indexed element.
So I have mentioned that still we can add the elements. yeah it is a old news
 
Rojan punn
Greenhorn
Posts: 17
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HashMap map<String ,Card> =new HasHMap<String,Card>();

puclic class card{

private String deck;

getDeck(){
}
setDeck(String strdeck)
this.deck=strdeck;
}

}

card crd=new card();
crd.setDeck("deck1");

map.put("deck name ",crd);

this runs fast and consume less memory . if you have lots of arraylist searching and elements on your way..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic