• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Array and ArrayList

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mag Yeoh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic