java二维数组遍历的2种代码
作者:郑清 时间:2022-05-03 08:52:50
二维数组遍历:
思想:
1.先将二维数组中所有的元素拿到
2.再将二维数组中每个元素进行遍历,相当于就是在遍历一个一维数组
第一种方法:双重for循环
//遍历二维数组
public class Traverse_a_two_dimensional_array {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = new int[2][3];//动态创建:2个元素(外围数组),每一个元素中各有3个元素(内围数组)
arr[0] = new int[]{1,2,3};//给第1个元素(外围数组),赋值1,2,3
arr[1][0] = 22;//给第2个元素中的第1个元素赋值22
arr[1][1] = 13;//给第2个元素中的第2个元素赋值13
arr[1][2] = 81;//给第2个元素中的第3个元素值81
for(int i = 0;i < arr.length;i++){
//System.out.println(arr[i]);//arr中元素:2个数组的地址
//遍历arr[0],arr中元素第一个数组
for(int j = 0;j < arr[i].length;j++){
System.out.print(arr[i][j] + ",");
}
}
}
}
运行结果图:
第二种方法:增强for循环foreach
//遍历二维数组
public class Traverse_a_two_dimensional_array {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr = {{65,6},{12,1,45,23},{0,-45,1}};//静态创建
for (int[] is : arr) {
for (int i : is) {
System.out.print(i + ",");
}
}
}
}
运行结果图:
来源:https://blog.csdn.net/qq_38225558/article/details/81663838
标签:java,二维数组,遍历
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android实现支付宝支付密码输入界面
2023-06-20 14:57:15
![](https://img.aspxhome.com/file/2023/8/83508_0s.gif)
Java for循环标签跳转到指定位置的示例详解
2022-10-04 17:52:31
Android中TextView动态设置缩进距离的方法
2023-08-07 09:52:00
![](https://img.aspxhome.com/file/2023/4/86754_0s.png)
springboot @Async 注解如何实现方法异步
2023-11-18 18:16:03
Android仿简书搜索框效果的示例代码
2023-06-18 16:02:58
![](https://img.aspxhome.com/file/2023/7/83527_0s.gif)
JAVA内存模型(JMM)详解
2023-11-23 16:54:32
![](https://img.aspxhome.com/file/2023/5/59975_0s.png)
Android应用开发SharedPreferences存储数据的使用方法
2023-06-23 16:02:47
![](https://img.aspxhome.com/file/2023/0/82410_0s.jpg)
Java实现FTP上传与下载功能
2021-09-22 18:28:51
![](https://img.aspxhome.com/file/2023/0/62340_0s.jpg)
java 动态生成SQL的实例讲解
2021-05-25 18:26:06
JavaSE static final及abstract修饰符实例解析
2021-07-01 10:54:54
![](https://img.aspxhome.com/file/2023/9/62129_0s.png)
Java基础教程之接口的继承与抽象类
2023-11-06 20:47:22
![](https://img.aspxhome.com/file/2023/5/58335_0s.jpg)
java中的session对象及其常用方法小结
2023-11-25 02:31:33
详解Java中对象池的介绍与使用
2023-07-25 13:13:48
Spring注解@Scope原理及用法解析
2023-12-06 14:08:17
java中关于深拷贝的几种方式总结
2023-12-13 17:39:41
![](https://img.aspxhome.com/file/2023/4/62054_0s.png)
Mybatis中resultMap的Colum和property属性详解
2023-09-16 11:17:33
![](https://img.aspxhome.com/file/2023/8/58538_0s.png)
Java调用Shell命令和脚本的实现
2023-11-29 00:59:09
springboot读取application.yaml文件数据的方法
2023-09-06 05:29:24
![](https://img.aspxhome.com/file/2023/6/63086_0s.jpg)
springboot+springmvc+mybatis项目整合
2023-07-16 03:13:57
![](https://img.aspxhome.com/file/2023/1/61251_0s.png)
Java二维数组简单定义与使用方法示例
2022-01-28 08:31:37