-1

I have a List which I need to convert to an int array (int[])

Currently I am doing this:

List<Integer> filters = new ArrayList<Integer>();
// add some elements to filters
int[] filteres = new int[filters.size()];
for(Integer i=0 ; i<filters.size(); i++) 
    filteres[i] = filters.toArray(
                  new Integer[filters.size()])[i].intValue();

I think this looks like a messy workaround and that should be somehow else to do this. So is there a better way to make such conversion?

Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • `filters.toArray(new Integer[filters.size()])[i]` - this is entirely unnecessary and wasteful; someone needs to learn to index into a [List](http://docs.oracle.com/javase/7/docs/api/java/util/List.html) (alternatively, skip indexing entirely with an enhanced for loop) – user2864740 May 29 '14 at 23:31
  • Also, your title lies. The operation is List to int[], not Integer[] to int[]. – user2864740 May 29 '14 at 23:34

4 Answers4

2

This can be simplified using the List.get() from the List interface:

int[] filteres = new int[];
for(int i=0 ; i<filters.size(); i++) 
   //auto unboxing takes care of converting Integer to int for you, if it's not null.
    filteres[i] = filters.get(i); 
amit
  • 175,853
  • 27
  • 231
  • 333
  • 1
    The enhanced for loop would be a better choice (since it uses an iterator) as `get(int index)` can be slow for certain List implementations (LinkedList) – Steve Kuo May 30 '14 at 13:48
2

See How to convert List<Integer> to int[] in Java? (which is a duplicate by all means; see this answer in particular).

However, for sake of discussion, consider this similar alternative and notes.

List<Integer> filters = getFilters();

// Arrays must be created with a size: the original code won't compile.
int[] ints = new int[filters.size()];

// Use an Enhanced For Loop if an index lookup into the source
// is not required; i is merely a result of needing to index the target.
int i = 0;
for(Integer filter : filters) {
    // Just use auto unboxing Integer->int; no need for intValue()
    ints[i++] = filter;
}

The original code is terrible because filters.toArray(new Integer[filters.size()])[i] creates a new array from the list each loop before the index operation. This makes the complexity O(n^2)+ just for the copy! While this can be fixed by replacing the offending expression with filters.get(i), an indexing operation can be skipped entirely in this case.

Using an enumerable approach similar to shown above (i.e. with an enhanced for loop) has the advantage that it will continue to work efficiently over source collections which are not fast-indexable such as a LinkedList. (Even using the filters.get(i) replacement, a LinkedList source collection would result in a O(n^2) complexity for the transformation - yikes!)

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
1

Instead of this:

filteres[i] = filters.toArray(
              new Integer[filters.size()])[i].intValue();

You can just retreive the element like this:

filteres[i] = filters.get(i);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
1
int[] filteres = new int[filters.size()];
        for(int i =0;i<filters.size();i++){
            filteres[i]=filters.get(i);
        }
Andrea T
  • 3,035
  • 4
  • 23
  • 39