Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Why doesn't Arrays.toList work?

Thread view: 
Chris Smowton - 10 Jul 2006 12:10 GMT
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
Thomas Hawtin - 10 Jul 2006 12:39 GMT
> I've seen a few things on the internet say that if you want a list from
> an array, you should go
[quoted text clipped - 5 lines]
> 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.<int[]>asList(new int[][] { test });

> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Chris Smowton - 10 Jul 2006 12:47 GMT
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...
Red Orchid - 11 Jul 2006 07:47 GMT
"Chris Smowton" <chris@smowton.net> wrote or quoted in
Message-ID: <1152532064.789695.220220@m73g2000cwd.googlegroups.com>:

> 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.
Dave Glasser - 10 Jul 2006 16:39 GMT
Thomas Hawtin <usenet@tackline.plus.com> wrote on Mon, 10 Jul 2006
12:42:58 +0100 in comp.lang.java.programmer:

>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.
Thomas Hawtin - 10 Jul 2006 19:43 GMT
> Thomas Hawtin <usenet@tackline.plus.com> wrote on Mon, 10 Jul 2006
> 12:42:58 +0100 in comp.lang.java.programmer:
[quoted text clipped - 5 lines]
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Dave Glasser - 10 Jul 2006 20:28 GMT
Thomas Hawtin <usenet@tackline.plus.com> wrote on Mon, 10 Jul 2006
19:47:32 +0100 in comp.lang.java.programmer:

>> Thomas Hawtin <usenet@tackline.plus.com> wrote on Mon, 10 Jul 2006
>> 12:42:58 +0100 in comp.lang.java.programmer:
[quoted text clipped - 8 lines]
>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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.