当前位置:首页>开发>正文

如何遍历二维数组的一列 二维遍历数组呢

2023-07-29 22:03:11 互联网 未知 开发

 如何遍历二维数组的一列 二维遍历数组呢

如何遍历二维数组的一列

int arr[][]= {{3,4,5,6},{7,8,9},{10,11}}
for (int i = 0 i < arr.length i ) {
for (int j = 0 j < arr[i].length j ) {
System.out.print(arr[i][j] " ")//遍历每一个一维数组
}
System.out.println()//换行
/*输出结果:3 4 5 6
7 8 9
10 11 */
}
}

二维遍历数组呢?

public static void main(String[] args) { int[][] arrays = new int[2][3] int levelOneLength = arrays.length // 第一维度数组长度 for (int i = 0 i < levelOneLength i) { int[] levelTwoArr = arrays[i] // 获取第二维度数组 int levelTwoLength = levelTwoArr.length // 第二维度数组长度 // 遍历第二维度数组 for (int j = 0 j < levelTwoLength j) { System.out.println("Array[" i "][" j "] = " levelTwoArr[j]) } } // 其实多维度数组的操作也是一样的,比如三维,四维等等 // int[][][] threeLevelArray = new int[1][2][3] // 三维数组 // int[][][][] fourLevelArray = new int[1][2][3][4] // 四维数组 // ......}

如何遍历二维指针数组?

只要写个双重循环就可以实现,n维数据就可以用n重循环来实现。举一个2维的例子,可以借鉴一下,例如
int line =0
int a[2][2] = {1,2,3,4}
for (int i = 0 i<2 i)
{
for (int j = 0 j<2 j)
{
cout << a[i][j] <<
line
}
cout << endl
}

java纵向遍历二维数组

package test.first

/**
* 数组的纵向迭代
*
* @author Administrator
*
*/
import java.util.Random
import java.util.Arrays
public class Testone {
public static void main(String[] args) {
Random random=new Random()
int [][] array=new int[5][5]
for(int i=0i<5i ){
for(int j=0j<5j ){
array[i][j]=random.nextInt(100)
System.out.print(array[i][j] " ")
}
System.out.print(" ")

}
System.out.println("----------纵向输出的---------")
int j=0
for(int i=0i<5i ){
while(j<5){
System.out.print(array[j][i] " ")
j
}
System.out.print(" ")
j=0
}

}
}

C 源代码问题:遍历二维数组

#include
using namespace std
void main()
{
int i,j,m,n,s
cin>>m>>n
int *p,**a
a=(int **)new int*[m]
for ( i=0i for ( i=0,p=a[0]i>(*p)
//for ( i=0,p=a[0]i for ( i=s=0,p=a[0]i cout<}

最新文章