该代码为锯齿状数组提供 NullPointerException
The code gives NullPointerException for jagged array
代码在注释行中给出了 NullPointerException。我找不到问题所在。
package com.lambda.classes;
import java.util.Random;
public class Lambda {
public static void main(String []args)
{
int array[][]=new int[5][];
Random r=new Random();
Random r2=new Random();
for(int i=0;i<5;i++){
int x=r.nextInt(10);
for(int j=0;j<x;j++)
{
int y=r2.nextInt(200);//this line gives a null pointer exception
array[i][j]=y;
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i+"\t");
}
System.out.println();
}
Random x=new Random();
System.out.println(x.nextInt(10));
System.out.println(x.nextInt(10));
}
}
您在这一行中的代码错误 'array[i][j]=y;'。
因为它'int array[][]=new int[5][];'
您也需要为内部数组设置大小。
像这样
public static void main(String[] args) {
int array[][] = new int[5][];
Random r = new Random();
Random r2 = new Random();
for (int i = 0; i < 5; i++) {
int x = r.nextInt(10);
array[i] = new int[x];
for (int j = 0; j < x; j++) {
int y = r2.nextInt(200);
array[i][j] = y;
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i + "\t");
}
System.out.println();
}
Random x = new Random();
System.out.println(x.nextInt(10));
System.out.println(x.nextInt(10));
}
实际上我正在尝试为此实现一个锯齿状数组,所以用 a[5][100] 之类的东西初始化数组有什么意义,如果我没记错的话 java声明一些东西,比如 int array[][] = new int[5][];完全没问题,感谢您的回答 (y) :)
现在我已经在不使用额外 space 的情况下让它工作了,就好像我可以不喜欢 int[5][100];我可能最终会使用额外的 space 但感谢该初始化点,它可以使用列表数组轻松完成......谢谢你们:)
int array[][]=new int[5][];
Random r=new Random();
for(int i=0;i<5;i++){
int x=r.nextInt(10);
array[i]=new int[x];
for(int j=0;j<x;j++)
{
array[i][j]=r.nextInt(200);
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}
代码在注释行中给出了 NullPointerException。我找不到问题所在。
package com.lambda.classes;
import java.util.Random;
public class Lambda {
public static void main(String []args)
{
int array[][]=new int[5][];
Random r=new Random();
Random r2=new Random();
for(int i=0;i<5;i++){
int x=r.nextInt(10);
for(int j=0;j<x;j++)
{
int y=r2.nextInt(200);//this line gives a null pointer exception
array[i][j]=y;
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i+"\t");
}
System.out.println();
}
Random x=new Random();
System.out.println(x.nextInt(10));
System.out.println(x.nextInt(10));
}
}
您在这一行中的代码错误 'array[i][j]=y;'。 因为它'int array[][]=new int[5][];'
您也需要为内部数组设置大小。
像这样
public static void main(String[] args) {
int array[][] = new int[5][];
Random r = new Random();
Random r2 = new Random();
for (int i = 0; i < 5; i++) {
int x = r.nextInt(10);
array[i] = new int[x];
for (int j = 0; j < x; j++) {
int y = r2.nextInt(200);
array[i][j] = y;
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i + "\t");
}
System.out.println();
}
Random x = new Random();
System.out.println(x.nextInt(10));
System.out.println(x.nextInt(10));
}
实际上我正在尝试为此实现一个锯齿状数组,所以用 a[5][100] 之类的东西初始化数组有什么意义,如果我没记错的话 java声明一些东西,比如 int array[][] = new int[5][];完全没问题,感谢您的回答 (y) :)
现在我已经在不使用额外 space 的情况下让它工作了,就好像我可以不喜欢 int[5][100];我可能最终会使用额外的 space 但感谢该初始化点,它可以使用列表数组轻松完成......谢谢你们:)
int array[][]=new int[5][];
Random r=new Random();
for(int i=0;i<5;i++){
int x=r.nextInt(10);
array[i]=new int[x];
for(int j=0;j<x;j++)
{
array[i][j]=r.nextInt(200);
}
}
for (int[] is : array) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}