ArrayList to array

N

Novello

Hi,

I got a problem with toArray method on ArrayList object.

ArrayList<String> _hidden = new ArrayList<String>();
String[] hidden = new String[_hidden.size()];
hidden = (String[]) _hidden.toArray();

what's wrong?
thanks
 
I

Ingo R. Homann

Hi,
Hi,

I got a problem with toArray method on ArrayList object.

ArrayList<String> _hidden = new ArrayList<String>();
String[] hidden = new String[_hidden.size()];
hidden = (String[]) _hidden.toArray();

try this instead:

ArrayList<String> _hidden = new ArrayList<String>();
....
String[] hidden = _hidden.toArray(new String[_hidden.size()]);

Hth,
Ingo
 
R

Roedy Green

ArrayList<String> _hidden = new ArrayList<String>();
String[] hidden = new String[_hidden.size()];
hidden = (String[]) _hidden.toArray();


See http://mindprod.com/jgloss/arraylist.html for how to do it.

1. you created a new empty hidden, then immediately threw it away.

2. What you should do with the empty hidden is pass it to toArray to
fill. Otherwise, because of type erasure, the only thing toArray can
produce is an Object[] which CANNOT be cast to a String[] EVEN IF all
the objects are Strings!!

When you do it correctly, the (String[]) cast is not needed.

See http://mindprod.com/jgloss/gotchas.html#ARRAYSTOREEXCEPTION
for a general discussion of the problem without the complication of
generics.
 
T

Thomas Hawtin

Ingo said:
I got a problem with toArray method on ArrayList object.

ArrayList<String> _hidden = new ArrayList<String>();
String[] hidden = new String[_hidden.size()];
hidden = (String[]) _hidden.toArray();


try this instead:

ArrayList<String> _hidden = new ArrayList<String>();
...
String[] hidden = _hidden.toArray(new String[_hidden.size()]);

I usually write it as:

private static final String[] EMPTY_STRING = new String[0];
....
String[] hidden = this.hidden.toArray(EMPTY_STRING);

It's probably slower, but there is less pissing about. You also get to
share zero length arrays.

Tom Hawtin
 
R

Roedy Green

String[] hidden = this.hidden.toArray(EMPTY_STRING);

It's probably slower, but there is less pissing about. You also get to
share zero length arrays.

here is the code for how toArray works:

public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;


If your passed-in array is too small, it has to create new one using
reflection, which is a notorious snail.
 
T

Thomas Hawtin

Roedy said:
String[] hidden = this.hidden.toArray(EMPTY_STRING);

It's probably slower, but there is less pissing about. You also get to
share zero length arrays.


here is the code for how toArray works:

public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;

I am familiar with the code.
If your passed-in array is too small, it has to create new one using
reflection, which is a notorious snail.

I get around 1000 cycles slower on my machine. Or about four times
slower at copying short, but not empty, lists. It's twice as fast for
empty lists.

Such code is executed rarely enough that I don't care, unless a problem
is shown. I'd much rather have neat code.

Tom Hawtin
 
R

Roedy Green

I am familiar with the code.

Those two sentences are not congruent. If you were familiar with the
code, you would not have said "probably". In any case, you were not
the only intended audience.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top