Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Classes for Arrays

 
manahei
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
TIA
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that you would need to create an Integer[] array from your int[] array before you can pass it as an Object[] array.
 
manahei
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul nailed it.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic