陷入2D阵列

时间:2015-10-28 21:20:54

标签: java for-loop multidimensional-array

我试图写一个程序来读取一个文本文件(很好用),其中包含一个像这样的双打列表:

int number;
while( fscanf(in, "%d", &number) == 1 ){
    a = realloc(a, sizeof(int)*(size+1));
    if ( a == NULL )
    {
       // Problem.
       exit(0);
    }
    a[size] = number;
    printf("a[i]=%d\n", a[size]);
    size++;
}

_

1.0, 2.0, 3.0
4.0, 5.0, 6.0
7.0, 8.0, 9.0

这就是我的目标:

public static void main(String[] args) {

    Scanner inputStream = null;

    try {
        inputStream = new Scanner(new FileInputStream("tstc.txt"));
    }
    catch (FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.out.println("Program aborted.");
        System.exit(0);
    }

    double[][] arrayNums = new double[3][3];

    for(int sec = 0; sec < 3; sec++) {
        while(inputStream.hasNextLine()) {
            String line = inputStream.nextLine();
            String[] lineList = line.split(", ");
            int pos = 0;

            for(int motor = 0; motor < 3; motor++) {
                double lineDouble = Double.parseDouble(lineList[pos]);
                arrayNums[motor][sec] = lineDouble;
                pos+=1;
            }
        }
    }
    System.out.println(Arrays.deepToString(arrayNums));

但这就是我所得到的:

    [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

我已经看到我的问题在于 sec 变量保持不变,但我不确定如何重新排列代码而不必更改整个代码结构。

任何提示都将不胜感激。

3 个答案:

答案 0 :(得分:2)

这应该像预期的那样工作。

    int pos = 0;
    while(inputStream.hasNextLine()) {
        String line = inputStream.nextLine();
        String[] lineList = line.split(", ");

        for(int motor = 0; motor < 3; motor++) {
            double lineDouble = Double.parseDouble(lineList[motor]);
            arrayNums[pos][motor] = lineDouble;

        }
        pos++;
    }
System.out.println(Arrays.deepToString(arrayNums));

不要忘记删除第一个循环。

答案 1 :(得分:1)

int sec = 0;
while (inputStream.hasNextLine()) {
    String line = inputStream.nextLine();
    String[] lineList = line.split(", ");

    for (int i = 0; i < lineList.length; i++) {
        arrayNums[sec][i] = Double.parseDouble(lineList[i]);
    }

    sec++;
}

如果您没有预料到文件中的行数(即文件中的行数多于数组中的行数),则会在内部for循环中抛出异常

答案 2 :(得分:1)

您需要删除while循环。

这会给你预期的结果:[[1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]]

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class ReadMatrix {

    public static void main(String[] args) {

        String fileName = "tstc.txt";
        int nRow = 3;
        int nCol = 3;
        double[][] arrayNums = new double[nRow][nCol];

        try (Scanner inputStream = new Scanner(new FileInputStream(fileName))) {

            for (int i = 0; i < nRow; i++) {
                String line = inputStream.nextLine();
                String[] lineList = line.split(", ");

                for (int j = 0; j < nCol; j++) {
                    double lineDouble = Double.parseDouble(lineList[j]);
                    arrayNums[i][j] = lineDouble;
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            System.out.println("Program aborted.");
            return;
        }
        System.out.println(Arrays.deepToString(arrayNums));
    }
}