为了处理数组元素,我们经常使用for循环或每个循环,因为数组中的所有元素都是相同的类型,并且数组的大小是已知的。假设我们有一个由5个元素组成的数组,我们可以将该数组的所有元素打印为:
public class ProcessingArrays {
public static void main(String args[]) {
int myArray[] = {22, 23, 25, 27, 30};
for(int i = 0; i<myArray.length; i++) {
System.out.println(myArray[i]);
}
}
}输出结果
22 23 25 27 30