从列表中填充2D数组

3

我有一个文件,其中包含以下数据:

4
5
0 2

0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3

第一行是节点数,第二行是边数,第三行包含了拥有特殊约束的节点。

然而,我使用以下代码将其从文件中读取到两个列表values和nodes中,其中values列表包含边(例如:0 1 0.6),而nodes列表则分别包含第三行的值(4, 5, {0,2})。

我需要构建两个数组,一个等效于邻接矩阵,它长这样:

int graph[][] = {
            {0, 6, 2, 5},
            {6, 0, 0, 8},
            {2, 0, 0, 3},
            {5, 8, 3, 0}
        };

另一个数组是特殊节点的ID,即:
int  special [] ={0,2};

我可以从文件中读取数值,并将它们分别放在两个列表nodes和values中,如下所示:

nodes列表包含:4,5,0,2 values列表包含:0,1,0.6,0,2,0.2,0,3,0.5,1,3,0.8,2,3,0.3

我声明了一个叫做graph2的二维数组来保存来自这些列表的值。但问题是我无法找到关系以便将数据从values列表填充到适合graph2 2d数组中。它必须像graph数组一样,但其初始化需要动态地从该文件中进行。

因此,基本上我需要创建两个数组:一个像graph数组(2d数组),另一个像special数组(1d)。根据该文件,我必须这样做。提前感谢您。

我的代码:

List<Double> values = new ArrayList<>();
        List<Integer> nodes = new ArrayList<>();
        int c = 0;
        File file = new File("text.txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                if (c <= 2) {

                    String[] str = text.split(" ");
                    for (int i = 0; i < str.length; i++) {
                        if (str[i].trim().length() > 0) {
                            nodes.add(Integer.parseInt(str[i]));
                        }
                    }
                    c++;
                } else {

                    String[] str = text.split(" ");
                    for (int i = 0; i < str.length; i++) {
                        if (str[i].trim().length() > 0) {
                            values.add(Double.parseDouble(str[i]));
                        }
                    }
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                System.out.print(e);
            }
        }
        double graph2 [] []  =new double [nodes.get(0)][nodes.get(0)]; 

我不明白你想做什么,你想直接从文件中读取到你的二维数组吗? - Joachim Huet
@JoachimHuet 请检查我的编辑。 - Subhi
你是否为这个问题的下一部分创建了一个新账户:https://stackoverflow.com/questions/47265100/how-to-read-this-from-file-and-get-each-value-alone - achAmháin
@notyou 是的,因为该账户已达到了问题限制。 - Subhi
您的约束节点位于nodes.get(2),而graph2缺少第二个参数为1。您已经在列表中拥有完整的值列表,也许您想在填充网格之前初始化graph2。 - Marcos Vasconcelos
@JoachimHuet 非常感谢您。 - Subhi
1个回答

0
以下代码将从您的文件创建2D数组(而不是邻接图,您可以轻松地自行创建)。 nodes仅包含特殊节点,而graph2将包含边缘描述符。
(注:我没有测试过,如果有问题请告诉我)
List<Double> values = new ArrayList<>();
List<Integer> nodes = new ArrayList<>();
int c = 0;
File file = new File("text.txt");
BufferedReader reader = null;

double[][] graph2 = null;    

try {
    reader = new BufferedReader(new FileReader(file));
    String text = null;
    while ((text = reader.readLine()) != null) {
        // The first line gives the number of nodes (You will use to create the int[][] graph = new int[nOfNodes][nOfNodes];)
        if (c == 0) {
            numberOfNodes = Integer.parseInt(text.trim());
        }
        // The second one gives the number of edges
        else if (c == 1) {
            nOfEdges = Integer.parseInt(text.trim());
            graph2 = new double[nOfEdges][3];
        }
        // And third the list of special nodes
        // `nodes` will now contains only your special constrained one
        else if (c == 2) {
            String[] str = text.split(" ");
            for (int i = 0; i < str.length; i++) {
                if (str[i].trim().length() > 0) {
                    nodes.add(Integer.parseInt(str[i]));
                }
            }
        } else { // Then you have your edges descriptors
            String[] str = text.split(" ");
            for (int i = 0; i < str.length; i++) {
                if (str[i].trim().length() > 0) {
                     graph2[c-4][i] = Double.parseDouble(str[i]);                     
                }
            }
        }
        c++;
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        System.out.print(e);
    }
}

然后,要创建您的图形,您可以将其初始化为零,然后循环遍历graph2以填充它。


首先感谢您,但是这段代码是错误的,在这一行中有一个错误 graph2[c - 3][i] = Double.parseDouble(str[i]);。 - Subhi
主线程中的异常:"main" java.lang.ArrayIndexOutOfBoundsException: 5 位于javaapplication6.JavaApplication6.main函数 - Subhi
由于空格的存在,所以你需要将 c-3 改为 c-4。这样应该就可以正常工作了。 - Joachim Huet
@SUBHIALMOHTASIB 你可以使用我的更新答案,我这次测试过了,对我有效。 - Joachim Huet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接