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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array and ArrayList" Watch "Array and ArrayList" New topic
Author

Array and ArrayList

Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 42
any different array and array list or list???

if i wanna retrieve database data and display in website right....what should i use to put in my data......array, arrayList or list....
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

An array has a fixed size that's determined when it's created. You can think of an ArrayList as an array that re-sizes itself as needed. Unlike an array, an ArrayList cannot hold primitive values (without wrapping them as objects).

List is an interface implemented by ArrayList.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 42
Thanks Marc...
if my object got 2 attribute, one is countryName and another is countryPopulation.....i want to retrieve this object to store the value into arrayList.....it is right to use arrayList???this is my first time using java and jsp and db4o database as my project....i very new in all this language.....

Thanks
Mingwei Jiang
Ranch Hand

Joined: Feb 19, 2004
Posts: 63
I think it's OK to use ArrayList. For example, you first retrieve the data from database and wrap the countryName and countryPopulation attributes into a Country object, then you can add this country object into a list of countries.

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

An ArrayList would probably work, but whether it's a good choice depends on how you intend to use this data.

You might want to review Sun's tutorial on Collections -- especially the part on implementations.
[ November 15, 2006: Message edited by: marc weber ]
Mag Yeoh
Ranch Hand

Joined: Oct 08, 2006
Posts: 42
Thanks....

but i having error for this.....

can you see my code................i still cannot print out the object value...i only print out the object address only....

this is my retrieve data code...



can i put String[] to replace Object[]???Actually what is Object[] this mean.....another question is what is ArrayList<String> this mean???
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Mag Yeoh:
...i still cannot print out the object value...i only print out the object address only.... can i put String[] to replace Object[]???Actually what is Object[] this mean.....another question is what is ArrayList<String> this mean???

An Object[] is an array that hold references to Objects. This means that when you add something to an array, the reference is upcast to type Object. So in order to use something in in an Object array, you normally need to downcast the reference back to its true type (for example, (MyType)array[x]...).

Typically, arrays are created with the desired type in mind. So yes, if your array is intended to hold Strings, then you should probably use String[] instead of Object[].

In Collections (for example, an ArrayList), all references are automatically upcast to type Object. Prior to the introduction of generics in Java 5, it was left to the programmer to know what was being added to a collection and ensure that it was being correctly downcast when used. But with generics, the type can be specified. For example, ArrayList<String> is an ArrayList that holds Strings. (Behind the scenes, the references are still upcast to type Object. But the generic type allows the compiler to check what's actually going in, and automaticaly insert the correct downcasting.)

If your references are printing as class names and hashCodes, this suggests that the toString() method has not been overridden.

But the question here is what type of objects does your ObjectSet contain?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Array and ArrayList