aspose file tools*
The moose likes Performance and the fly likes Array vs ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Array vs ArrayList" Watch "Array vs ArrayList" New topic
Author

Array vs ArrayList

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 19, 2011
Posts: 152

Ok, ill stick with the ArrayList so. Thanks for the info!

Alan
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7045
    
  16

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


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Mar 10, 2010
Posts: 12
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

Joined: Mar 02, 2012
Posts: 50
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


regards,
Victor M. Pereira
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sumiran Pradhan wrote:Definitely use the List over arrays.


AGAIN...

It depends on the situation.

*sigh*
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 03, 2012
Posts: 3
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

Joined: Sep 20, 2010
Posts: 3370
    
    9
Besides, you can't 'add' to an array. You can only 'set'.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 29, 2011
Posts: 17


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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array vs ArrayList
 
Similar Threads
Is it a bug in jdk1.3?
Loading 2-dimensional array with values
How I get byte[ ] from Object?
Help please!
WA #1.....word association