-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
  • 14,165
  • 5
  • 50
  • 83
mero mero
  • 9
  • 1
  • 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);
Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
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,455
  • 12
  • 17
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,797
  • 2
  • 27
  • 31
Duaa S
  • 49
  • 4