How to convert int array (int[]) into list in Java

2 Answers

0 votes
import java.util.List;
import java.util.Arrays;
 
public class MyClass {
    public static void main(String args[]) {
       int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
         
       List<Integer> list = Arrays.stream(arr).boxed().toList();
         
       list.forEach(System.out::println);
    }
}
 
 
 
 
/*
run:
 
1
2
3
4
5
6
7
8
 
*/

 



answered Jul 20, 2020 by avibootz
edited Jun 12, 2023 by avibootz
0 votes
import java.util.List;
import java.util.ArrayList;
 
public class MyClass {
    public static void main(String args[]) {
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
         
        List<Integer> list = new ArrayList<Integer>(arr.length);
        
        for (int n : arr) {
            list.add(n);
        }
         
        list.forEach(System.out::println);
    }
}
 
 
 
 
/*
run:
 
1
2
3
4
5
6
7
8
 
*/

 



answered Jun 12, 2023 by avibootz

Related questions

1 answer 135 views
2 answers 115 views
1 answer 210 views
2 answers 285 views
1 answer 160 views
1 answer 150 views
1 answer 122 views
...