Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二维数组中搜索值_Java_Arrays_Search_Multidimensional Array - Fatal编程技术网

Java 在二维数组中搜索值

Java 在二维数组中搜索值,java,arrays,search,multidimensional-array,Java,Arrays,Search,Multidimensional Array,我遇到麻烦的部分是在破折号之后。我需要在二维数组中搜索两个用户输入的数字。按照我的方式,它总是说值存在,并且没有正确指定数字所在的索引 import javax.swing.*; import java.util.*; public class labActivityArrays{ public static void main(String[] args){ String rowS; rowS = JOptionPane.showInputDialog("Number of rows:

我遇到麻烦的部分是在破折号之后。我需要在二维数组中搜索两个用户输入的数字。按照我的方式,它总是说值存在,并且没有正确指定数字所在的索引

import javax.swing.*;
import java.util.*;

public class labActivityArrays{

public static void main(String[] args){

String rowS;
rowS = JOptionPane.showInputDialog("Number of rows: ");
int row =  Integer.parseInt(rowS);
String columnS;
columnS = JOptionPane.showInputDialog("Number of columns: ");
int column = Integer.parseInt(columnS);

String initialS;
initialS = JOptionPane.showInputDialog("Initial value: ");
int initial =  Integer.parseInt(initialS);

int[][] arr = new int [row][column];

int temp = initial; 

for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     arr[i][j] = temp;
     temp += 1;
  }
}

for(int i=0; i < row; i++){
  for(int j=0; j < column; j++){
     System.out.print(arr[i][j] + " ");
  }
  System.out.println();
}

String check1S;
check1S = JOptionPane.showInputDialog("First value to check for: ");
int check1 =  Integer.parseInt(check1S);

String check2S;
check2S = JOptionPane.showInputDialog("Second value to check for: ");
int check2 =  Integer.parseInt(check2S);
import javax.swing.*;
导入java.util.*;
公共类LabActivityArray{
公共静态void main(字符串[]args){
字符串行;
rowS=JOptionPane.showInputDialog(“行数:”);
int row=Integer.parseInt(行);
字符串列;
columnS=JOptionPane.showInputDialog(“列数:”);
int column=Integer.parseInt(列);
字符串首字母;
缩写=JOptionPane.showInputDialog(“初始值:”);
int initial=Integer.parseInt(缩写);
int[]arr=新int[行][列];
int temp=初始温度;
对于(int i=0;i
//-------------------------------------------------

boolean found = false;    

int i = 0;
int j = 0;

//for the first value
for(i = 0; i < row; i++){
  for(j=0; j < column; j++){
     if(arr[i][j] == check1){
        found = true;      
        break;
     }
  }
}

if(found){
  System.out.println("Found " + check1 + " at index [" + i + "][" + j + "].");
}
else{
  System.out.println(check1 + " is not in this array.");
}

//for the second value
for(i = 0; i < row; i++){
  for(j=0; j < column; j++){
     if(arr[i][j] == check2){
        found = true;      
        break;
     }
  }
}

if(found){
  System.out.println("Found " + check2 + " at index [" + i + "][" + j + "].");
}
else{
  System.out.println(check2 + " is not in this array.");
}


}
}
boolean-found=false;
int i=0;
int j=0;
//对于第一个值
对于(i=0;i
这是我在破折号后的新代码:

boolean found = false;
int i = -1;
int j = -1;

//for the first value  
while(!found && i++ < row){
  j = -1;
  while(!found && j++ < column){
     found = (arr[i][j] == check1);
  }
}

if(found){
  System.out.println("Found " + check1 + " at index [" + i + "][" + j + "].");
}
else{
  System.out.println(check1 + " is not in this array.");
}

//for the second value

while(!found && i++ < row){
  j = -1;
  while(!found && j++ < column){
     found = (arr[i][j] == check2);
  }
}

if(found){
  System.out.println("Found " + check2 + " at index [" + i + "][" + j + "].");
}
else{
  System.out.println(check2 + " is not in this array.");
}
boolean-found=false;
int i=-1;
int j=-1;
//对于第一个值
而(!找到(&i++<行){
j=-1;
while(!found&&j++<列){
发现=(arr[i][j]==check1);
}
}
如果(找到){
System.out.println(“在索引[“+i+”][“+j+”])处找到“+check1+”;
}
否则{
System.out.println(check1+“不在此数组中”);
}
//对于第二个值
而(!找到(&i++<行){
j=-1;
while(!found&&j++<列){
found=(arr[i][j]==check2);
}
}
如果(找到){
System.out.println(“在索引[“+i+”][“+j+”])处找到“+check2+”;
}
否则{
System.out.println(check2+“不在此数组中”);
}

中断
只会中断内部循环。外面的那个一直在走

尝试:

int i = -1;
int j = -1;
while(!found && ++i < row) {
    j = -1;
    while(!found && ++j < column) {
        found = (arr[i][j] == check);
    }
}
inti=-1;
int j=-1;
而(!found&&++i<行){
j=-1;
而(!found&&++j<列){
发现=(arr[i][j]==检查);
}
}

这适用于第一个值,但第二个值将显示第一个值的索引位置。如果第一个值不在数组中,则会弹出此错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:5在labActivityArrays.main(labActivityArrays.java:85)对于第二个值,您必须重新执行所有操作:
found=false;i=-1;j=-1
您有一个异常,因为您使用了
i++
而不是
++i