Why doesn't Arrays.toList work?

C

Chris Smowton

I've seen a few things on the internet say that if you want a list from
an array, you should go

int[] test = {1, 2, 3, 4, 5};

List myList = Arrays.toList(test);

However, on Java 5 this doesn't seem to work, it tries to produce a
List<Integer[]> instead of a List<Integer>.

Obviously I could fix this by calling toList(1, 2, 3, 4, 5), but this
is hardly practical when a function returns a long array.

For the record, the specific application is I want an ArrayList of the
UTF-8 bytes of some string.

Thanks if anyone can help,

Chris
 
T

Thomas Hawtin

Chris said:
I've seen a few things on the internet say that if you want a list from
an array, you should go

int[] test = {1, 2, 3, 4, 5};

List myList = Arrays.toList(test);

However, on Java 5 this doesn't seem to work, it tries to produce a
List<Integer[]> instead of a List<Integer>.

List<int[]>. int[] isn't assignment compatible to Object[], therefore
the line is treated as:

List myList = Arrays. said:
Obviously I could fix this by calling toList(1, 2, 3, 4, 5), but this
is hardly practical when a function returns a long array.

If you want to use asList, you will need to box the primitive individually:

final int num = test.length;
final Integer[] boxed = new Integer[num];
for (int ct=0; ct<num; ++ct) {
boxed[ct] = test[ct];
}
final List<Integer> myList = Arrays.asList(boxed);

Alternative write, or acquire, an implementation of List that backs to
an int[] (and boxes/unboxes) instead of a reference array.
For the record, the specific application is I want an ArrayList of the
UTF-8 bytes of some string.

So shouldn't that be List<Byte>? Arrays.asList actually returns a
(package? private) java.util.Arrays.ArrayList not a java.util.ArrayList.

Tom Hawtin
 
C

Chris Smowton

Indeed, I am using <Byte>, that was just a quickly scrawled example.

Thanks for your reply; I thought it might be something along those
lines, it just struck me as odd you couldn't just go

ArrayList<Byte> utf8bytes = new
ArrayList<Byte>(myString.getBytes("UTF-8"));

since Java is usually so heavy on convenience methods.

Apparently nobody at Sun thought that might be handy...
 
D

Dave Glasser

Tom Hawtin

Are things really that bad in southern England, that a decent Java
programmer will have a hard time finding work? I'm not implying
anything by asking, it just seems that the economy there would have to
be pretty dismal for that to be the case.
 
T

Thomas Hawtin

Dave said:
Are things really that bad in southern England, that a decent Java
programmer will have a hard time finding work? I'm not implying
anything by asking, it just seems that the economy there would have to
be pretty dismal for that to be the case.

I think it's more a case of managers wanting to copy the decisions of
others rather than making any themselves.

Tom Hawtin
 
D

Dave Glasser

I think it's more a case of managers wanting to copy the decisions of
others rather than making any themselves.

Uh, thanks, but I'm not following what you said.
 
R

Red Orchid

Message-ID: said:
ArrayList<Byte> utf8bytes = new
ArrayList<Byte>(myString.getBytes("UTF-8"));

If it is possible not to use 'List', it will be more better to
use the following.

- Trove collections (http://trove4j.sourceforge.net/)
or
- ByteBuffer, IntBuffer (java.nio.*)
or
- Your implementation of primitive list.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top