The Artima Developer Community
Sponsored Link

Java Answers Forum
Array vs. ArrayList (or whatelse choice I have?)

4 replies on 1 page. Most recent reply: Aug 23, 2003 1:37 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Dor

Posts: 13
Nickname: eis
Registered: Jul, 2003

Array vs. ArrayList (or whatelse choice I have?) Posted: Aug 22, 2003 5:33 AM
Reply to this message Reply
Advertisement
Hello all!

I am trying to implement something reading in a large number of parameters (the exact number is not pre-known, below 300, say). The parameter itself is actually the lowest layer of this hierarchy.

Then I have the second layer called Cell, each containing this many Paras. Now comes the third layer 'ControlCenter', each containing between 200 and 300 Cells, as well as below 100 Paras of itself. And I need to have around 100 ControlCenter (to be organized in a certain way too) in the program.

I wonder what is(are) the appropriate way(s) to collect these Para, Cell, ControlCenter. The thing in common is I don't know the exact number of instances for each of them, but it will be several hundred as I have noted already; also it wouldn't be required to take any instance away from the collection once it's added.

Two of the choices I have been thinking of so far are Array and ArrayList. The argument for Array is clear, the thing against it is I shall always be checking about the non-null end; on the other hand, ArrayList is more flexibel, but also expensive in the sense of accessing speed and so on.

My question, what would you suggest me to use, array, arraylist, or something else? And your reason please.

Thanks a lot!


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Array vs. ArrayList (or whatelse choice I have?) Posted: Aug 22, 2003 6:23 AM
Reply to this message Reply
If your application is multi-threaded, Vector might be needed.

Basically, I would suggest you use ArrayList. If once your program is completed, it needs to be optimised, all interfaces should be able to remain the same and you can re-code it with arrays. However, I doubt if that'd be necessary.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Array vs. ArrayList (or whatelse choice I have?) Posted: Aug 22, 2003 9:23 AM
Reply to this message Reply
The Properties class could handle your parameters that you read in.
You could create Cell and ControlCenter classes which have Properties member variable to hold them.

Properties are just key, value pairs that can be read or written to or from a test file.

Guillaume Taglang

Posts: 18
Nickname: gouyou
Registered: Jul, 2003

Re: Array vs. ArrayList (or whatelse choice I have?) Posted: Aug 22, 2003 2:39 PM
Reply to this message Reply
I do not think we know egnough of your application to decide, it really depends of what you want to do with your application: searching through your lists, lots of additions/deletions, multithreading, ...

My usual advice in this case is go for the List interface (with your implementation of choice). When you will know the usage pattern of the application, then make your choice ... it might be that the good solution for you are arrays, but LinkedList might more suitable, or even sets. It really depends on what people will do with this structure, and not on this implementation vs. that other one.

Cheers,
Guillaume

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Array vs. ArrayList (or whatelse choice I have?) Posted: Aug 23, 2003 1:37 AM
Reply to this message Reply
If you look at ArrayList internal it actually stores the elements in an Object array. To be precise it is
 private transient Object elementData[]; 

Also if you take for example the add method in the array list it looks like
public boolean add(Object o) {
	ensureCapacity(size + 1);  // Increments modCount!!
	elementData[size++] = o;
	return true;
 }

All these operation anyway you have to do even if you are handling your params with array. for example you have to maintain a counter, you have to add to the array and stuff like that. So I don't think for basic operations like add and get ArrayList is going to be expensive than an array.


However when you use an ArrayList it can perform all the other sort of functions like converting to array adding a collection which as you have mentioned will be expensive. But you can choose not to use them if you don't want to but using an ArryaList you will always have these functionality with you whenever you need them.


Moreover another reason to go for ArrayList is because in J2SE 1.5 in the future there will be Generics introduced and also there will a new kind of for loop introduced to iterate over collections. Which will make your life even easier.


Another problem with ArrayList now which is not there in array is when you take out the Objects in ArrayList you have to cast them which might reduce some performance but then again in 1.5 the Generics should solve this problem.


So if you ask me between ArrayList and array my vote is for ArrayList.

Flat View: This topic has 4 replies on 1 page
Topic: trying to submit form on another site from my JSP Previous Topic   Next Topic Topic: How to truncate a string, and read parts of it in as integer?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use