How to initialize an ArrayList with zeros in Java

2 Answers

0 votes
import java.util.ArrayList; 
import java.util.Arrays; 
  
public class MyClass {
    public static void main(String args[]) {
        ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(0, 0, 0, 0, 0)); 
 
        System.out.println(al);
    }
}
  
  
  
  
/*
run:
  
[0, 0, 0, 0, 0]
  
*/

 



answered Sep 9, 2022 by avibootz
0 votes
import java.util.ArrayList; 
import java.util.List; 
import java.util.Collections; 

public class MyClass {
    public static void main(String args[]) {
        List<Integer> list_al = new ArrayList<Integer>(Collections.nCopies(10, 0));
   
        System.out.println(list_al);
    }
}
   
   
   
/*
run:
   
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
   
*/

 



answered Sep 9, 2022 by avibootz

Related questions

1 answer 261 views
1 answer 305 views
1 answer 321 views
1 answer 259 views
4 answers 582 views
582 views asked Jan 29, 2019 by avibootz
1 answer 223 views
...