How to declare and initialize ArrayList with numbers in Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.ArrayList;

public class MyClass {
    public static void main(String args[]) {
        ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(3, 8, 7, 1, 4, 5));
        
        list.forEach(n -> System.out.println(n));
    }
}


/*
run:

3
8
7
1
4
5

*/

 



answered Jul 25, 2020 by avibootz

Related questions

1 answer 205 views
1 answer 113 views
2 answers 212 views
2 answers 211 views
1 answer 208 views
1 answer 227 views
...