What kind of {List, Array, Vector} should I use?

R

Ramon F Herrera

My applet has several JComboBoxes which are filled at
start() time with items provided by the web server.

I need some advise to help me determine what is the best linear
class (List? Array? Vector?, some subclass of those?) to fill
the arrays. The object requirements are: the object should
grow dynamically, it should be sorteable and should be assignable
to a ComboBoxModel. The array has the handy feature of easy sorting:

Arrays.sort(myArray);

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector. Likewise, I have been unable
to assign a List to a ComboBoxModel.

Thanks you very much for your expert assistance.

Regards,

-Ramon F. Herrera
 
V

VisionSet

Ramon F Herrera said:
My applet has several JComboBoxes which are filled at
start() time with items provided by the web server.

I need some advise to help me determine what is the best linear
class (List? Array? Vector?, some subclass of those?) to fill
the arrays. The object requirements are: the object should
grow dynamically, it should be sorteable and should be assignable
to a ComboBoxModel. The array has the handy feature of easy sorting:

Arrays.sort(myArray);

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector. Likewise, I have been unable
to assign a List to a ComboBoxModel.


Vectors are old hat (I was going to say legacy, but since I'm not precisely
sure what that means I won't), ArrayList is the prefered alternative.
You have a Constructor accepting a Vector - ignore it.
You have a Constructor that accepts an Object[], use this.

ArrayList myList = new ArayList();

// add stuff to it.

Collections.sort(myList); // or use the version with a Comparator, for
custom sorting.

YourObjType[] myArray = myList.toArray(new YourObjType[0]);


List is an Interface not a class.
 
M

Mike Schilling

Ramon F Herrera said:
My applet has several JComboBoxes which are filled at
start() time with items provided by the web server.

I need some advise to help me determine what is the best linear
class (List? Array? Vector?, some subclass of those?) to fill
the arrays. The object requirements are: the object should
grow dynamically, it should be sorteable and should be assignable
to a ComboBoxModel. The array has the handy feature of easy sorting:

Arrays.sort(myArray);

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector. Likewise, I have been unable
to assign a List to a ComboBoxModel.

Thanks you very much for your expert assistance.

Vector vs. ArrayList:

Vector is smewhat obsolete, since it predates the collections hierarchy.
(It has since been retrofitted to implement List.) All public Vector
methods are synchronized, which is unnecessary overhead unless you'll be
using it in more than one thread.

ArrayList vs. Array

Arays are fixed-sized while ArrayLists are extensible. It's sometime useful
to use an ArrayList to collect a list of objects, and copy them to an array
with the toArray() method.

By the way, Lists can be sorted using the Collections.sort() method. (Both
the Arrays and Collections classes are collections of useful utility
functions written as static methods. )
 
J

John C. Bollinger

Ramon said:
My applet has several JComboBoxes which are filled at
start() time with items provided by the web server.

I need some advise to help me determine what is the best linear
class (List? Array? Vector?, some subclass of those?) to fill
the arrays. The object requirements are: the object should
grow dynamically, it should be sorteable and should be assignable
to a ComboBoxModel. The array has the handy feature of easy sorting:

Arrays.sort(myArray);

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector. Likewise, I have been unable
to assign a List to a ComboBoxModel.

Vectors _are_ Lists (since Java 1.3, I believe, but perhaps only since
1.4). The sorting method you are looking for is Collections.sort(List).

If you really wanted to use a different kind of List then you would need
to create your own implementation of ComboBoxModel; the easiest way
would be to subclass AbstractListModel.


John Bollinger
(e-mail address removed)
 
S

Sudsy

Ramon said:
My applet has several JComboBoxes which are filled at
start() time with items provided by the web server.

I need some advise to help me determine what is the best linear
class (List? Array? Vector?, some subclass of those?) to fill
the arrays. The object requirements are: the object should
grow dynamically, it should be sorteable and should be assignable
to a ComboBoxModel. The array has the handy feature of easy sorting:

Arrays.sort(myArray);

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector. Likewise, I have been unable
to assign a List to a ComboBoxModel.

Since Vector implements the Collection interface, you can create
a new TreeSet which (according to the javadocs):
"guarantees that the sorted set will be in ascending element order,
sorted according to the natural order of the elements".

TreeSet ts = new TreeSet( your_vector );

Then you can obtain an Iterator and walk through in ascending order.

As to the ComboBoxModel, that's an interface, not a class.
DefaultComboBoxModel is a class which implements ComboBoxModel.
One constructor takes an array of Object while another takes
a Vector.

See? Javadocs are your friend!
 
R

Roedy Green

but the fixed size is a problem. On the other hand, the vector
is dynamic and I can add items at a given place, but I cannot
figure out how to sort a vector.

The basic rules are:
if you know the size in advance, use array.
If you need to grow, use ArrayList.
If you need simultaneous access by more than one thread use Vector.
If you need the Applet to work in ancient browsers, use Vector.

From there see http://mindprod.com/jgloss/sort.html on how to sort.
All can be sorted.


the asArray and asList methods can be useful in creating compatible
plumbing.

see http://mindprod.com/jgloss/collection.html
 
R

Roedy Green

You have a Constructor that accepts an Object[], use this.

ArrayList myList = new ArayList();

oops. Object[] and ArrayList are not interchangeable.

You can use ArrayList.toArray to get an array of objects.
 
V

VisionSet

Roedy Green said:
You have a Constructor that accepts an Object[], use this.

ArrayList myList = new ArayList();

oops. Object[] and ArrayList are not interchangeable.

Read the post again, Mr Snippy ;-)

& yeah 4NT looks great, a recent addition to the glossary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top