How to initialize all array elements with specific number in Java

1 Answer

0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int [] array = new int[10];
        
        Arrays.fill(array, 42);

        for (int n : array) {
            System.out.println(n);
        }
    }
}



 
/*
run:
 
42
42
42
42
42
42
42
42
42
42
 
*/

 



answered Oct 4, 2021 by avibootz
...