javaforeach打印
时间: 2023-12-05 22:41:28 浏览: 63
以下是Java中使用foreach循环打印数组的例子:
```java
public class forEach {
public static void main(String[] args) {
String[] hobby = {"imooc", "telephone", "zhangjie"};
for (String str : hobby) {
System.out.println(str);
}
}
}
```
输出结果为:
```
imooc
telephone
zhangjie
```
以下是Java中使用foreach循环处理二维数组并打印的例子:
```java
public class forEach {
public static void main(String[] args) {
int[][] data = new int[10][5];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = i * j;
}
}
for (int[] data1 : data) {
for (int data2 : data1) {
System.out.printf("%d\t", data2);
}
System.out.println();
}
}
}
```
输出结果为:
```
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
0 5 10 15 20
0 6 12 18 24
0 7 14 21 28
0 8 16 24 32
0 9 18 27 36
```
阅读全文