-1
如何可以通过foreach循环方法打印一维数组的元素一维阵列打印
public class SingleArrayPrac {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4 };
// for(int row : array) This way i can print array but i want to print through method, How?
// System.out.println(row);
for (int print : array) {
printRow(print);
}
}
public static void printRow(int row) {
for (int print : row) { // Error at Row
System.out.println(print);
}
}
}
在printRow方法中row是一个整数,你会如何遍历一个整数?你只需要打印参数。 – lateralus
您的'printRow'方法中的'row'变量已包含数组元素。所以你只需要用'println'在'printRow'中打印'row', –