Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting in List of lists using Array.asList in java

Tags:

java

I am coding in java and i have three arrays also i have a List of lists.

int[] recovery=bl.generateRecovery();
    int[] periods = bl.generatePeriods(recovery);
    int[] deadlines =  bl.generateDeadlines(periods);

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

Now i want to add the arrays as lists in the list.

I tried using:

temp.add(Arrays.asList(recovery));

But it failed. Can someone tell me how to do it ??

like image 637
Magnum Avatar asked Nov 28 '25 21:11

Magnum


2 Answers

You can make use of Arrays.stream on int[] boxed and collected as List as:

temp.add(Arrays.stream(recovery).boxed().collect(Collectors.toList()));
like image 179
Naman Avatar answered Dec 01 '25 09:12

Naman


Arrays.asList(recovery) will give you a List<int[]> object, not List<Integer>. Try using Integer[] recovery instead.

Edit: You might want to have your methods like generateRecovery etc. to return List<Integer> instead of int[]. Arrays are not flexible and it is usually better to use List.

like image 29
Dabiuteef Avatar answered Dec 01 '25 09:12

Dabiuteef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!