gpt4 book ai didi

java - 如何在java中使两个二维数组相等?

转载 作者:行者123 更新时间:2023-12-01 19:45:38 25 4
gpt4 key购买 nike

我创建了一些代码,如下所示,但是当我尝试使主 2D 数组等于新的 2D 数组内容时,它不起作用。我对二维数组很陌生,因此我们将不胜感激。

public class Simple2dArray {

private int row= 0;
private int column= 0;
private String [][] mainArray= null;

}

public static void main(String[] args){
Simple2dArray array= new Simple2dArray (); // (class name is Simple2dArray)
array.create2d(); //(calls out create2d)

// array.addStartPosition(); //(calls out addStartPosition)


}

private void create2d(){
String line;


try {
FileReader fr= new FileReader("data.txt");
BufferedReader br= new BufferedReader(fr);


while ((line = br.readLine()) != null){

ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
String[] splitter= line.split(" ");
arrayL.add(splitter);
}//



}

}
br.close();
}catch (IOException e) {
System.out.println("error");
}


}

最佳答案

这是我的解决方案。您混淆了一些代码行,将它们放在 while block 中,并在 2dArray 填充期间忘记了 doubleFor。在下面的代码中,您可以直接在 main2dArray 上工作。

首先,

private static String [][] main2dArray= null;

然后,在create2d()中,读取文件后,添加arrayL中的所有splitter:

       ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
String[] splitter= line.split(" ");
arrayL.add(splitter);
}

现在您可以使用 double-For 复制 main2dArray 中的所有值:

       main2dArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<main2dArray.length; i++) {
for(int j=0; j<main2dArray[0].length;j++)
main2dArray[i][j] = arrayL.get(i)[j];
}

所以,当你调用array.create2d();(我没有修改它)时,输出是:

[[25, S, 92, 78, 2], [14, 21, 52, 2, 4], [2, 61, 11, 24, 7], [56, 45, 16, 57, 4], [32, 15, 98, 13, 6]]

字符“S”位于正确的位置!

为了避免误解,这里是带有注释的完整代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Ciao {
private static String [][] main2dArray= null;

public static void main(String[] args) throws IOException{
array.create2d(); //(calls out create2d)
addStartPosition();
}

private static void create2d(){
String line;
try {
FileReader fr= new FileReader("data.txt");
BufferedReader br= new BufferedReader(fr);

ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
String[] splitter= line.split(" ");
arrayL.add(splitter);
}//while

main2dArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<main2dArray.length; i++) {
for(int j=0; j<main2dArray[0].length;j++)
main2dArray[i][j] = arrayL.get(i)[j];
}//for
br.close();
}catch (IOException e) {
System.out.println("error");
}//try-catch
}//create2d

private static void addStartPosition() {
main2dArray[0][1] = "S";
System.out.println(Arrays.deepToString(main2dArray));
}//addStartPosition

}//class

PS:我将您的方法设置为“静态”,并因此删除了行 Simple2dArray array= new Simple2dArray ();,但没有必要这样做。

<小时/>

EDIT1:我已经尝试过你的新代码(你可以从它的格式来看我并没有真正接触过它),如果我在之前添加 int counter=0; ,它就可以工作try-catch block :

<小时/>

EDIT2:你不能在第一时间使用line = br.readLine(),否则内部cicle将覆盖line的值。根据你的平原,你必须使用另一个条件......例如:

   boolean flag=true; //condition
while (flag){

switch (counter) {
case (1):
System.out.println ("case 1");
break;
case (2):
System.out.println ("case 2");
break;
case (3):
System.out.println("case 3");
break;
default:
ArrayList<String[]> arrayL= new ArrayList<>();
while ((line = br.readLine()) != null){
//System.out.println(line);
String[] splitter= line.split(" ");
arrayL.add(splitter);
}//
mainArray= new String[arrayL.size()][arrayL.get(0).length];
for(int i = 0; i<mainArray.length; i++) {
for(int j=0; j<mainArray[0].length;j++)
mainArray[i][j] = arrayL.get(i)[j];
} //for
break;
}//switch
counter++;

//for example, we can interrupt the cycle
flag=false; //You must set your own condition!
}//while

现在你的输出将是:

screen

...这应该是正确的!

关于java - 如何在java中使两个二维数组相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53504712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com