Creating JLists with Object arrays or Vectors

When I create a Jlist with

jlist.setListdata(object[])

the jlist.getmodel().getSize() method always returns the object arrays dimension versus the size of the list.

When I use a vector instead of the object array it seems to work.

Just wanted to know if there is anyway to get the true size of a list created with an object array?

[363 byte] By [MikeCobba] at [2007-9-28 12:07:33]
# 1
Why would there be a difference between the two numbers?
DrClapa at 2007-7-12 3:06:20 > top of Java-index,Archived Forums,Java Programming...
# 2

When I use an object array to initialize my Jlist, I get the entire dimension object array returned as the size of my Jlist.

for example

object[] test=new Object[15];

....

test[1]=some object;

test[2]=some object;

//then I add it to the jlist

jlist.setListData(test)

//now I want to use a for loop to extract the list data

size=jlist.getModel().getSize()

size will be 15 everytime. I want it to be 2. I dont want it to count the null values.

When I replace the Object Array with a vector:

Vector test=new Vector(1);

it works correctly.

what do you think?

MikeCobba at 2007-7-12 3:06:20 > top of Java-index,Archived Forums,Java Programming...
# 3

I see. You allocated your array too big. Use the correct size for your array -- in your example that would be 3 instead of 15. If you don't know in advance what the correct size is going to be then a Vector (preferably an ArrayList) would be the correct object to use anyway, so the question shouldn't arise.

DrClapa at 2007-7-12 3:06:20 > top of Java-index,Archived Forums,Java Programming...
# 4
Thanks. Vectors are working well.
MikeCobba at 2007-7-12 3:06:20 > top of Java-index,Archived Forums,Java Programming...