Java method example for initializing and filling arrays
•
Java
Initialize and populate the array
import java.util.Arrays; public class ArrayFilling { public static void main(String[] args) { int[] scoreArr = new int[8]; // 创建一个大小为8的数组 Arrays.fill(scoreArr,0); // 将数组使用数字 0 进行填充 for (int i = 0; i < scoreArr.length; i++) { System.out.print(scoreArr[i] + " "); } System.out.print(""); Arrays.fill(scoreArr,2,6,1);// 将索引从 2 到 6 使用数字 1 进行填充 for (int i = 0; i < scoreArr.length; i++) { System.out.print(scoreArr[i] + " "); } } }
Print results:
PS: array filling in Java
Through the array of the Java uill class. Fill (array name, value) method and array Fill (arrayname, starting index, ending index, value) code:
import java.util.Arrays; public class ArrayFill { public static void main(String[] args) { int array[] = new int[6]; Arrays.fill(array,100); for (int i = 0;i < array.length; i++) { System.out.print(array[i]+" "); } System.out.println(); Arrays.fill(array,3,50); for (int i = 0; i < array.length; i++) { System.out.print(array[i]+" "); } } }
result:
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码