Create an array with n copies of the same value/object?

后端 未结 7 1712
刺人心
刺人心 2020-12-16 11:29

I want to create an array of size n with the same value at every index in the array. What\'s the best way to do this in Java?

For example, if n

7条回答
  •  自闭症患者
    2020-12-16 11:42

    Or you can do it in the low level way. Make an array with n elements and iterate through all the element where the same element is put in.

    int[] array = new int[n];
    
    for (int i = 0; i < n; i++)
    {
        array[i] = 5;
    }
    

提交回复
热议问题