-1

I have that method :

 public void checkCategories(Integer... indices) {
    .....
  } 

and the input of this method is lists of Integer lists.

My question is how to convert the lists of Integer lists to Integer arrays be passed in that method ?

rkosegi
  • 12,129
  • 5
  • 46
  • 75
mero mero
  • 9
  • 2

3 Answers3

1

You can convert List to array by method toArray()

List<Integer> integerList = new ArrayList<Integer>();

Integer[] flattened = new Integer[integerList.size()];
integerList.toArray(flattened);

checkCategories(flattened);
XIII-th
  • 4,820
  • 2
  • 30
  • 52
0

If you need to flatten list of lists into array you can try something like that:

public static void main(String [] args) {
    List<List<Integer>> integers = Lists.newArrayList(
            Lists.newArrayList(1, 2, 3, 4),
            Lists.newArrayList(5, 3, 6, 7)
    );

    Integer[] flattened = integers.stream()
            .flatMap(Collection::stream)
            .toArray(Integer[]::new);

    checkCategories(flattened);
}

public static void checkCategories(Integer... indices) {
    for (Integer i : indices) {
        System.out.println(i);
    }
}

It prints:

1
2
3
4
5
3
6
7
g-t
  • 1,265
  • 10
  • 15
0

You can convert the list to an ArrayList as follows:

userList = new ArrayList<Integer>();

And then, you can convert that list to an Array using

int[] userArray = userList.toArray(new int[userList.size()]);
Jaymin Panchal
  • 2,664
  • 2
  • 24
  • 28
Duaa S
  • 49
  • 4