Possible Duplicate:
How to create ArrayList (ArrayList<T>) from array (T[]) in Java
How to implement this method:
List<Integer> toList(int[] integers) {
???
//return Arrays.asList(integers); doesn't work
}
-
Is there a reason for the second part of this question, before I edit it out?DJClayworth– DJClayworth2011年07月08日 16:20:03 +00:00Commented Jul 8, 2011 at 16:20
-
@DJClayworth: I think, no. I've already flagged the question and explaned that I met some difficulties during submitting this question.Roman– Roman2011年07月08日 16:21:08 +00:00Commented Jul 8, 2011 at 16:21
-
2@Merlyn: Not a duplicate. Primitive arrays are different from Object arraysAlexander Pogrebnyak– Alexander Pogrebnyak2011年07月08日 16:32:18 +00:00Commented Jul 8, 2011 at 16:32
-
@Alexander: Wow. That's gross :) I can't rescind my vote, so I'll just delete my comment.Merlyn Morgan-Graham– Merlyn Morgan-Graham2011年07月08日 16:36:46 +00:00Commented Jul 8, 2011 at 16:36
-
Instead of being a duplicate of the given question, this is a duplicate of stackoverflow.com/q/1073919/1108305.M. Justin– M. Justin2023年06月14日 04:40:51 +00:00Commented Jun 14, 2023 at 4:40
6 Answers 6
There's probably a built-in method to do it somewhere* (as you note, Arrays.asList won't work as it expects an Integer[] rather than an int[]).
I don't know the Java libraries well enough to tell you where that is. But writing your own is quite simple:
public static List<Integer> createList(int[] array) {
List<Integer> list = new ArrayList<Integer>(array.length);
for (int i = 0; i < array.length; ++i) {
list.add(array[i]);
}
return list;
}
Obviously one downside of this is that you can't do it generically. You'll have to write a separate createList method for each autoboxed primitive type you want.
*And if there isn't, I really wonder why not.
1 Comment
new ArrayList<Integer>(array.length). Should keep Java from having to resize the collection to add elements.Use commons-lang3 org.apache.commons.lang3.ArrayUtils.toObject(<yout int array>) and then java.util.Arrays.asList(<>)
ArrayUtils.toObject() will copy the array, and Array.asList() will simply create list that is backed by new array.
int[] a = {1, 2, 3};
List<Integer> aI = Arrays.asList(ArrayUtils.toObject(a));
EDIT: This wont work if you want to add() new elements (resize) though the list interface, if you want to be able to add new elements, you can use new ArrayList(), but this will create one more copy.
2 Comments
List<Integer> xValues = Arrays.asList(new Integer[] { 150, 160, 175, 165, 165 });Integer[] literal, but not with an existing int[]. It can not be placed in the method skeleton from the OP.List<Integer> asList(final int[] integers) {
return new AbstractList<Integer>() {
public Integer get(int index) {
return integers[index];
}
public int size() {
return integers.length;
}
};
}
13 Comments
add(...) was implemented in AbstractList. My bad, thanks for the info!List<Integer> toList(int[] integers) {
// Initialize result's size to length of the incoming array
// this way it will not require reallocations
ArrayList<Integer> result = new ArrayList<Integer>( integers.length );
for ( int cur: integers )
{
result.add( Integer.valueOf( cur ) );
}
return result;
}
Comments
I do not think there is a quick way to do it unfortunately. I believe you will have to iterate the array and add it one by one.
Comments
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class Listing {
public static void main(String[] args) {
int[] integers = {1,2,3,4};
java.util.List<Integer> list = new ArrayList<Integer>();
for (int i=0; i< integers.length; i++)
{
list.add(integers[i]);
}
System.out.println(list);
}
}
Tested and working as expected!