wood burning stoves
The moose likes Beginning Java and the fly likes Wrapper Classes for Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Classes for Arrays" Watch "Wrapper Classes for Arrays" New topic

Wrapper Classes for Arrays


Joined: Jul 16, 2001
Posts: 3
Difficulty with the use of wrapper classes here.
I want to create a List full of integers that
comes from an int[]. Please help me understand how to
give Arrays.asList(Object[])this array:
private int[] xxx = {23, 45, 89, 54, 21};
I have tried String[] = no problem...
I have tried Integer[] = problem...
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Not sure exactly what your problem is. Are you trying to put the contents of the int array into an Integer array? If so, each element of the int array will need to be put into an Integer.
int[] a = {1,2,3,4};
Integer[] b = new Integer[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = new Integer(a[i]);
Kai Middleton

Joined: Jul 27, 2001
Posts: 12
I've had a similar though not identical situation. I'd like to do something like this:
ArrayList a = Collections.createList({1, 2, 3, 4});
That way I could write quickie test programs. Is there such a method?

<I>Kai M.</I>
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
It seems that you would need to create an Integer[] array from your int[] array before you can pass it as an Object[] array.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

Joined: Jul 16, 2001
Posts: 3
Paul nailed it.
I agree. Here's the link: http://aspose.com/file-tools
subject: Wrapper Classes for Arrays
It's not a secret anymore!