如何在Java中打印2D数组

huangapple go评论55阅读模式
英文:

how to print 2d array in java

问题

I'm here to assist with translation. Here's the translated code portion:

我需要在运行时提供行和列的值来打印一个二维数组。

```lang-java
package test;

import java.util.Scanner;

public class test1 {
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int rowsize = scan.nextInt();
		int i = 0; int j = 0;
		int rowarr[] = new int[rowsize];
		int colsize = scan.nextInt();
		int colarr[] = new int[colsize];
		int d[][] = new int[rowsize][colsize];
		for (i = 0; i < rowarr.length; i++) {
			rowarr[i] = scan.nextInt();
		}
		
		for (j = 0; j < colsize; j++) {
			colarr[j] = scan.nextInt();
		}
		
		for (int k = 0; k < rowarr.length; k++) {
			for (int m = 0; m < colarr.length; m++) {
				System.out.print(d[rowarr[k]][colarr[m]]);
			}
			System.out.println();
		}
	}
}

我在最后一行打印 d[rowarr[k]][colarr[m]] 时遇到了错误。是否有人可以提供建议?


<details>
<summary>英文:</summary>

I need to print a 2d array in which I will be giving the values for the row and column at run time.

```lang-java
package test;

import java.util.Scanner;

public class test1 {
	
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int rowsize=scan.nextInt();
		int i=0;int j=0;
		int rowarr[]=new int[rowsize];
		int colsize=scan.nextInt();
		int colarr[]=new int[colsize];
		int d[][]=new int[rowsize][colsize];
		for( i=0;i&lt;rowarr.length;i++) {
			rowarr[i]=scan.nextInt();
		}
		
		for( j=0;j&lt;colsize;j++) {
			colarr[j]=scan.nextInt();
		}
		
		for(int k=0;k&lt;rowarr.length;k++) {
			for(int m=0;m&lt;colarr.length;m++) {
				System.out.print(d[rowarr[k]][colarr[m]]);
			}
			System.out.println();
		}
	}
}

I'm getting an error in last line while printing d[rowarr[k]][colarr[m]]. Could anyone provide suggestions?

答案1

得分: 1

数组 d[][] 对于 d[0...rowsize-1][0...colsize-1] 索引是可访问的。

但是,如果您尝试访问任何其他负数索引或大于 rowsize-1 的索引,比如 d[rowsize+5][0],那么它会产生错误。

同样地,如果您尝试访问 d[0][colsize+2] 或类似的索引,或者 d[0][-3],也会产生错误。

因此,如果您想要访问 d[x][y](其中 x 和 y 是整数),则 x 和 y 必须遵循以下规则:

  1. 0 <= x < rowsize
  2. 0 <= y < colsize

请检查您是否正确访问了 d[][]。

英文:

As you are not providing any details about errors and constraints, I am answering on assumptions:

The array d[][] is accessible for d[0...rowsize-1][0...colsize-1] indices.

But, if you try to access any other index which in negative or which is greater than rowsize-1 like d[rowsize+5][0], then it'll produce error.

And likewise, if u try to access d[0][colsize+2] or something like that, or d[0][-3], it'll also give error.

So, if you want to access d[x][y] (where, x and y are integers) then x and y must follow the below rules:

  1. 0 <= x < rowsize
  2. 0 <= y < colsize

check if you're accessing d[][] properly...

答案2

得分: 0

你遇到了ArrayIndexOutOfBoundsException,因为:

System.out.println(d[rowarr[k]][colarr[m]])

尝试访问d[][]数组的元素,使用特定的rowarr[k]colarr[m]的值,超出了给定数组的范围。

例如,如果你将rowarr[0]填充为1colarr[0]填充为6,但是你生成了大小分别为2和3的rowarr[]colarr[]数组,那么在你的第一次迭代中,d[1][6](即d[rowarr[k]][colarr[m]])会越界,因为d[1]数组中没有第6个元素。

英文:

You're having an ArrayIndexOutOfBoundsException, because:

System.out.println(d[rowarr[k]][colarr[m]])

tries to access the element of d[][] array, with particular values of rowarr[k] and colarr[m], which goes outside the range of the given array.

For example, if you populate rowarr[0] with 1, colarr[0] - with 6, but you have generated rowarr[] and colarr[] arrays with the sizes of 2 and 3 respectively, then d[1][6] (at your first iteration - d[rowarr[k]][colarr[m]]), will be out of bounds, because there is no 6<sup>th</sup> element in the d[1] array.

答案3

得分: 0

我认为你的d数组仍然为空。它被初始化然后什么都没有发生。

英文:

Tell me if I'm wrong, but I think your d array remains empty. It's initialized and then nothing.

huangapple
  • 本文由 发表于 2020年7月31日 20:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63191576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定