簡體   English   中英

為什么不能在循環外訪問2D數組?

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

我在循環之外訪問2D數組(myArray)遇到麻煩。 我想使用其他方法來訪問它,但是我什至不能用這種方法來訪問它。 它在循環播放時可以正確打印,但測試打印

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

始終為null。 因此,就像數組實際上沒有填充一樣。 有人知道我在做什么錯嗎?

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;
}

正如您所擁有的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++;

        }
    }

因此,您的檢查始終返回null。

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

不要使用for和while循環在一起。 在第一個for循環中,while讀取所有文件內容,而所有其他數組位置保持為空。 嘗試以下方法:

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++;
}

我認為您代碼中的問題就在那里

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