提问人:Aman Nambisan 提问时间:7/17/2022 最后编辑:MasoudAman Nambisan 更新时间:7/18/2022 访问量:372
在 java 中接受和输入非矩形 2D 数组中的值 [复制]
Accepting & Inputing values in a non rectangular 2D array in java [duplicate]
问:
我正在尝试编写一个程序来搜索二维数组中数字的索引。在运行代码时,我不断遇到此异常:
线程“main”java.lang.NullPointerException 中的异常:无法存储到 int 数组,因为“local4/ [local5]”为 null
代码为:
import java.util.*;
public class indexSearch
{
public static void main(String[] args)
{
Scanner xyz=new Scanner(System.in);
int no1,no2;
System.out.println("Enter the first dimension of 2D array");
no1=xyz.nextInt();
int nos[][]=new int[no1][];
for(int i =0;i<nos.length ;i++)
{
System.out.println("Enter the second dimension corresponding to first index "+i);
int k=xyz.nextInt();
System.out.println("Enter the values into it now");
for(int j=0;j<k;j--)
{
nos[i][k]=xyz.nextInt();
}
}
int posi1=-1;int posi2=-1;
System.out.println("Enter the no you want to search");
int l=xyz.nextInt();
for(int i=0;i<nos.length;i++)
{
for(int j = 0; j < nos[i].length;j++)
{
if(nos[i][j]==l)
{
i=posi1;
j=posi2;
}
}
}
if(posi1!=-1&&posi2!=-1)
{
System.out.println("The index was"+posi1+" "+posi2);
}
else
{
System.out.println("The no doesnt exist");
}
}
}
我最近才开始学习数组,在二维数组中索引搜索数字的所有解决方案都是数组是矩形的,这不得不让我认为没有办法从用户值输入二维非矩形数组,其大小也来自用户。
答:
1赞
UltraDev
7/17/2022
#1
当您尝试插入值时,当前为 null,在获取值之前,将其初始化为 。nos[i]
nos[i] = new int[k];
与您的错误无关,我建议给变量提供更具描述性的名称,例如 代替 ,因为这使其他开发人员更容易理解您的代码。scanner
xyz
1赞
Ishadi Jayasinghe
7/17/2022
#2
代码中存在几个问题。
- nos[i] 为 null。因此,您可以将其初始化为 nos[i] = new int[k];当你得到 k 的值时
- 在 for 循环中,J 从 0 开始,你有 j--。必须是 J++。
- 您正在插入 nos[i][k],这会导致 ArrayIndexOutOfBounds 异常。应为 nos[i][j]=xyz.nextInt()。
下面给出了固定算法。
import java.util.*;
public class indexSearch
{
public static void main(String[] args)
{
Scanner xyz=new Scanner(System.in);
int no1,no2;
System.out.println("Enter the first dimension of 2D array");
no1=xyz.nextInt();
int nos[][]=new int[no1][];
for(int i =0;i<nos.length ;i++)
{
System.out.println("Enter the second dimension corresponding to first index "+i);
int k=xyz.nextInt();
nos[i] = new int[k];
System.out.println("Enter the values into it now");
for(int j=0;j<k;j++)
{
nos[i][j]=xyz.nextInt();
}
}
int posi1=-1;int posi2=-1;
System.out.println("Enter the no you want to search");
int l=xyz.nextInt();
for(int i=0;i<nos.length;i++)
{
for(int j = 0; j < nos[i].length;j++)
{
if(nos[i][j]==l)
{
i=posi1;
j=posi2;
}
}
}
if(posi1!=-1&&posi2!=-1)
{
System.out.println("The index was"+posi1+" "+posi2);
}
else
{
System.out.println("The no doesnt exist");
}
}
}
0赞
Jafar Sadik
7/17/2022
#3
代码中存在多个错误。首先,在此代码块中赋值、赋值和更改nos[i]
nos[i][k]=xyz.nextInt();
nos[i][j]=xyz.nextInt();
for(int i =0;i<nos.length ;i++)
{
System.out.println("Enter the second dimension corresponding to first index "+i);
int k=xyz.nextInt();
nos[i] = new int[k];
System.out.println("Enter the values into it now");
for(int j=0;j<k;j++)
{
nos[i][j]=xyz.nextInt();
}
//....
其次,改到这里:j--
j++
System.out.println("Enter the values into it now");
for(int j=0;j<k;j++)
{
nos[i][j]=xyz.nextInt();
}
第三,从这里改变:
i=posi1;
j=posi2;
对此:
posi1=i;
posi2=j;
完整的工作代码在这里:
import java.util.*;
public class indexSearch
{
public static void main(String[] args)
{
Scanner xyz=new Scanner(System.in);
int no1,no2;
System.out.println("Enter the first dimension of 2D array");
no1=xyz.nextInt();
int nos[][]=new int[no1][];
for(int i =0;i<nos.length ;i++)
{
System.out.println("Enter the second dimension corresponding to first index "+i);
int k=xyz.nextInt();
nos[i] = new int[k];
System.out.println("Enter the values into it now");
for(int j=0;j<k;j++)
{
nos[i][j]=xyz.nextInt();
}
}
int posi1=-1;int posi2=-1;
System.out.println("Enter the no you want to search");
int l=xyz.nextInt();
for(int i=0;i<nos.length;i++)
{
for(int j = 0; j < nos[i].length;j++)
{
if(nos[i][j]==l)
{
posi1=i;
posi2=j;
}
}
}
if(posi1!=-1&&posi2!=-1)
{
System.out.println("The index was"+posi1+" "+posi2);
}
else
{
System.out.println("The no doesnt exist");
}
}
}
与此问题无关,但请尝试为变量提供有意义的名称并修复代码的缩进,以便其他开发人员更容易理解您的代码并帮助您。
祝您编码愉快。
评论