37

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
}
asked Jul 8, 2011 at 16:15
5
  • Is there a reason for the second part of this question, before I edit it out? Commented 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. Commented Jul 8, 2011 at 16:21
  • 2
    @Merlyn: Not a duplicate. Primitive arrays are different from Object arrays Commented Jul 8, 2011 at 16:32
  • @Alexander: Wow. That's gross :) I can't rescind my vote, so I'll just delete my comment. Commented 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. Commented Jun 14, 2023 at 4:40

6 Answers 6

9

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.

answered Jul 8, 2011 at 16:24
Sign up to request clarification or add additional context in comments.

1 Comment

For efficiency's sake, i might recommend new ArrayList<Integer>(array.length). Should keep Java from having to resize the collection to add elements.
2

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.

answered Jul 8, 2011 at 16:41

2 Comments

You can do it directly with List<Integer> xValues = Arrays.asList(new Integer[] { 150, 160, 175, 165, 165 });
@AlexanderPacha Definitely, it works with an Integer[] literal, but not with an existing int[]. It can not be placed in the method skeleton from the OP.
2
List<Integer> asList(final int[] integers) {
 return new AbstractList<Integer>() {
 public Integer get(int index) {
 return integers[index];
 }
 public int size() {
 return integers.length;
 }
 };
}
ordnungswidrig
3,1971 gold badge19 silver badges29 bronze badges
answered Jul 8, 2011 at 16:27

13 Comments

There are a couple of big issues: (1) the list is backed by the array -- that is, if the array elements change, the list elements will too. (2) For larger ints, you'll get back a different Integer each time -- that is, you can not have any expectation of reference equality. Either of those might or might not be an issue, but they're semi-surprising behavior. Before i add (3), can Java actually do closures like that?
@cHao: 3. Yes, Java can do closures like that!
Technically this should then be named as List, at least when assuming that Java follows the same naming convention as .NET in this regard (as providing a differently-typed view on the same collection while to actually doing a conversion).
These are not closures. They are anonymous classes.
@Christoffer, ah, yes, then there's no problem: I thought add(...) was implemented in AbstractList. My bad, thanks for the info!
|
1
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;
}
answered Jul 8, 2011 at 16:26

Comments

0

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.

answered Jul 8, 2011 at 16:27

Comments

0
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!

answered Jul 8, 2011 at 16:44

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.