【问题标题】:Filling 2d Array from lists从列表中填充二维数组
【发布时间】:2017-11-13 14:41:39
【问题描述】:

我有一个包含以下数据的文件:

4
5
0 2

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

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

但是,我使用此代码将其从文件中读取到两个列表中,值和节点,其中值列表包含边(例如:0 1 0.6),而节点列表包含 3 行的值( 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};

我可以从文件中读取值并将它们分成以下形状的两个列表节点和值: 节点列表包含:4 ,5 ,0 ,2 值列表包含:0,1,0.6,0,2,0.2,0,3,0.5,1,3,0.8,2,3,0.3

我声明了一个名为 graph2 的二维数组,以保存来自列表的值。但问题是我找不到从值列表中填充数据以适合 graph2 2d 数组的关系。它必须像图形数组,但它的初始化将是从该文件动态的。

所以,基本上我需要的是创建两个数组:一个类似于图形数组(2d 数组),另一个类似于特殊数组(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)]; 

【问题讨论】:

  • 我不明白您要做什么,您想直接从二维数组中的文件中读取?
  • @JoachimHuet 请检查我的编辑。
  • 你为这个问题的下一部分创建了一个新帐户:stackoverflow.com/questions/47265100/…
  • @notyou 是的,因为该帐户已达到问题限制
  • 您的约束节点位于 nodes.get(2) 并且 graph2 错过了第二个参数为 1。并且您已经在列表中拥有完整的值列表,也许您想在之前初始化 graph2填充网格

标签: java arrays list


【解决方案1】:

以下代码将从您的文件创建 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);
    }
}

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

【讨论】:

  • 好吧,首先感谢,但是这段代码是错误的,这条线有错误 graph2[c - 3][i] = Double.parseDouble(str[i]);
  • 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 5 at javaapplication6.JavaApplication6.main
  • @SUBHIALMOHTASIB 由于空白处多了一行,所以您必须将c-3 更改为c-4。它应该工作
  • @SUBHIALMOHTASIB 你可以使用我更新的答案,我这次测试了它,它对我有用。
猜你喜欢
  • 1970-01-01
  • 2019-11-29
  • 2023-03-21
  • 2016-05-27
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2019-01-14
  • 1970-01-01
相关资源
最近更新 更多