简体   繁体   English

我的方法只返回从 while 循环中获得的最后一个值

[英]My method is returning only the last value that was gotten from the while loop

I'm working on an assignment were we have to get characters from a file and store it in a 2D array.我正在处理一项任务,我们必须从文件中获取字符并将其存储在二维数组中。 We're not allowed to post our code anywhere so i'll use a psuedocode.我们不允许在任何地方发布我们的代码,所以我将使用伪代码。 Let me know if it's not clear.如果不清楚,请告诉我。

fillFromFile (parameters){
    Scanner input = new Scanner(new FileReader(fileName));//from parameter
    char[][] returnChar = new char[parm][parm];//gotten from parameters
    String line = input.nextLine();
    String[] spliter = null;
    int count = 0;

    while (input.hasNextLine and count < row) {
        line = input.nextLine();
        spliter = line.split(" ", 2);
        count++;
        for (int i = 0; to returncharLength; increment i) {
            for (int j = 0; to returnchar[i]Length; increment j) {
                returnChar[i][j] = spliter[0].charAt(j);
            }
        }   
    }
    return returnChar;
}

If I print out from the method directly, the correct values are shown but we can't print from methods.如果我直接从方法中打印出来,则会显示正确的值,但我们无法从方法中打印。 There's a toString() method to handle that already.已经有一个 toString() 方法来处理它。

The 2D array being returned are just the last characters.返回的二维数组只是最后一个字符。 eg, if this is the expected value,例如,如果这是预期值,

r e
r e
o p
b n

this is what is being returned这就是被退回的东西

b n
b n
b n
b n

I've tried doing a deep copy, before and after returning it but there's no difference.我尝试过在返回之前和之后进行深层复制,但没有区别。 My deep copy method looks like this:我的深拷贝方法如下所示:

private static char[][] deepCopy (char [][] data){
    char [][] newArray = new char [//][//];
    for (int i = 0; i < length i++) {
        for (int j = 0; j < colLength; j++) {
            newArray[i][j] = data[i][j];
        }
    }
    return newArray;
}

I think the while loop is overwriting all the data before exiting.我认为while循环在退出之前会覆盖所有数据。 How can I fix this?我怎样才能解决这个问题?

If something is not clear, please let me know and i'll edit my question.如果有不清楚的地方,请告诉我,我会编辑我的问题。

I've also tried returning the 2d array at different parts of the method我也尝试在方法的不同部分返回二维数组

This is an example of how I'm using the returned 2d array这是我如何使用返回的二维数组的示例

variableX = new ClassName (2dArrayThatWasReturned); variableX = new ClassName (2dArrayThatWasReturned); return variableX;返回变量X;

-> variableX is of type ClassName and the constructor accepts a 2d array -> The 2D array that was passed will have it values assigned to an instance 2d array. -> variableX 是 ClassName 类型,并且构造函数接受 2d 数组 -> 传递的 2D 数组会将其值分配给实例 2d 数组。 The instance 2d array is the one that gets printed实例 2d 数组是被打印的数组

The problem is that your loop:问题是你的循环:

 for (int i = 0; to returncharLength; increment i) { for (int j = 0; to returnchar[i]Length; increment j) { returnChar[i][j] = spliter[0].charAt(j); } }

will override the whole array because you go from row 0 to returnchar.length , every time.将覆盖整个数组,因为你 go 从第0行到returnchar.length ,每次。 Instead you need to fill only the next row after the last one.相反,您只需要在最后一行之后填写下一行。 You can rewrite your for loops as follow:您可以按如下方式重写您的for循环:

for (int r=0; r<returnchar.length; r++) { // "r" for row
    String line = input.nextLine();
    String[] splitter = line.split(" ", 2);
    for (int c=0; c<returnchar[r].length; c++) { // "c" for column
        returnchar[r][c] = splitter[c];
    }
}

This is how to print a pretty Array 2D:这是如何打印一个漂亮的 Array 2D:

for (char[] x : returnChar) {
    System.out.println(Arrays.toString(x));
}

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

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