简体   繁体   English

为什么不能在循环外访问2D数组?

[英]Why can't I access my 2D array outside of the loop?

I'm having some trouble accessing my 2D array (myArray) outside of this the loop. 我在循环之外访问2D数组(myArray)遇到麻烦。 I want to access it using other methods, but I can't even access it in this method. 我想使用其他方法来访问它,但是我什至不能用这种方法来访问它。 It prints out correctly as it's looping, but the test print of 它在循环播放时可以正确打印,但测试打印

System.out.println(myArray[10][2]);

is always null. 始终为null。 So it's like the array isn't actually filling or something. 因此,就像数组实际上没有填充一样。 Anyone know what I'm doing wrong here? 有人知道我在做什么错吗?

package titanic;

import java.io.*;
import java.util.*;

public class Titanic {


public static final int ROW = 1309;
public static final int COLUMN = 6;
public static String [][] myArray = new String[ROW][COLUMN];


public static String[][] arraySetup(){

    int recordCounter = 0;
    String[][] myArray = new String[ROW][COLUMN];
    String[] name = new String [ROW];
    try {
        BufferedReader br = new BufferedReader(new FileReader("C:/Users/Tom/Desktop/Titanic.txt"));
        String line;
        for (int i = 0; i < 1309; i++){
        while((line = br.readLine()) != null){

            String tmp[] = line.split("\t");
                myArray[i][0] = tmp[0];
                myArray[i][1] = tmp[1];
                myArray[i][2] = tmp[2];
                myArray[i][3] = tmp[3];
                myArray[i][4] = tmp[4];
                myArray[i][5] = tmp[5];
                System.out.println("myArray[i][5] = " + myArray[i][5]);
                recordCounter++;

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(myArray[10][2]);

    System.out.println(recordCounter + " records.");
    return myArray;
}

As you have you while loop inside for loop that is used to for indexing your output array while loop always writes into the myArray[0][0] to myArray[0][5] 正如您所拥有的while for循环中的while循环用于索引输出数组, while循环始终写入myArray[0][0]myArray[0][5]

    for (int i = 0; i < 1309; i++){   // i is 0
    while((line = br.readLine()) != null){   // you go through all the lines while i is 0

        String tmp[] = line.split("\t");
            myArray[i][0] = tmp[0];
            myArray[i][1] = tmp[1];
            myArray[i][2] = tmp[2];
            myArray[i][3] = tmp[3];
            myArray[i][4] = tmp[4];
            myArray[i][5] = tmp[5];
            System.out.println("myArray[i][5] = " + myArray[i][5]);
            recordCounter++;

        }
    }

Because of that your check always returns null. 因此,您的检查始终返回null。

System.out.println(myArray[10][2]);

Don't use for and while loop together. 不要使用for和while循环在一起。 During the first for loop, your while reads all file contents and all other array positions remain empty. 在第一个for循环中,while读取所有文件内容,而所有其他数组位置保持为空。 Try this instead: 尝试以下方法:

int i=0;
while ((line = br.readLine()) != null){
    String tmp[] = line.split("\t");
     myArray[i][0] = tmp[0];
     myArray[i][1] = tmp[1];
     ...       
     myArray[i][5] = tmp[5];

     i++;
     System.out.println("myArray[i][5] = " + myArray[i][5]);
     recordCounter++;
}

I think the problem in your code is right there 我认为您代码中的问题就在那里

for (int i = 0; i < 1309; i++){ // you loop 1308 time
        while((line = br.readLine()) != null){ /* in each loop, you loop 
 until you have read all the file so you read 1308 time the file. But when 
you reach the end of file on the first iterration, it wont read the file on the 1307
 other iterration  and all data will be store in myArray[0][0-5]*/

            String tmp[] = line.split("\t");
                myArray[i][0] = tmp[0];
                myArray[i][1] = tmp[1];
                myArray[i][2] = tmp[2];
                myArray[i][3] = tmp[3];
                myArray[i][4] = tmp[4];
                myArray[i][5] = tmp[5];
                System.out.println("myArray[i][5] = " + myArray[i][5]);
                recordCounter++;

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM