-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 ?

0

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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()]);

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.