如何从文本文件中读取并将数据放入带有3个参数的2D数组中?

时间:2017-01-02 20:21:50

标签: java arrays

我有一个文本文件,其中的数据存储如下:

_IO_new_file_xsputn

第一行说明顶点数和边数,其他线说明两个顶点和该边的权重。我不知道如何在2D数组中存储除第一行之外的所有行,如下所示:

7,12
0,1,2
0,4,4
0,5,1
1,0,2
1,2,4
1,3,1
1,6,2
2,1,4
2,3,2
3,1,1
3,2,2
3,4,6
3,6,4
4,0,4
4,3,6
4,5,7
4,6,3
5,0,1
5,4,7
5,6,3
6,1,2
6,3,4
6,4,3
6,5,3

请帮忙!

2 个答案:

答案 0 :(得分:1)

您可以使用列表然后填充var updateArray = function(){ $scope.$apply(function () { $scope.products = listProductsService.query(); }); } 2D矩阵。代码很脏并且需要重构,但您$userManager = $this->get('fos_user.user_manager'); $entity->setPlainPassword("123"); $userManager->updateUser($entity); 将拥有文件int

中的值
edges

输出打印graph.txt变量

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        int a, b;
        int c, d, e;
        int[][] edges;
        ArrayList<ArrayList<Integer>> intlist = new ArrayList<ArrayList<Integer>>();
        int count = 0;
        try (BufferedReader br = new BufferedReader(new FileReader("/home/dac/gs-rest-service/gnu/src/main/java/gnu/graph.txt"))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            String[] lineVector;

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
                if (line == null)
                    break;

                String[] lineVector2;
                lineVector2 = line.split(",");

                //parsing the values to Integer
                c = Integer.parseInt(lineVector2[0]);
                d = Integer.parseInt(lineVector2[1]);
                e = Integer.parseInt(lineVector2[2]);

                int[] array = new int[3];

                array[0] = c;
                array[1] = d;
                array[2] = e;

                List<Integer> intList2 = new ArrayList<Integer>();
                for (int index = 0; index < array.length; index++) {
                    intList2.add(array[index]);
                }
                intlist.add(new ArrayList<Integer>(intList2));
                count++;

            }
            //System.out.println(intlist);
            int[] list;
            int index = 0;
            edges = new int[intlist.size()][count];
            for (ArrayList<Integer> b2 : intlist) {
                //necessary code here
                list = toIntArray(b2);
                edges[index] = list;
                index++;
            }
            System.out.println("edges:");
            for (int i = 0; i < edges.length; i++) {
                for (int j = 0; j < edges[i].length; j++) {
                    System.out.print(edges[i][j] + " ");
                }
                System.out.println();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }


    }

    static int[] toIntArray(List<Integer> list) {
        int[] ret = new int[list.size()];
        for (int i = 0; i < ret.length; i++)
            ret[i] = list.get(i);
        return ret;
    }
}

答案 1 :(得分:1)

因为您没有发布任何代码,我认为您不知道这是如何完成的,但不要期望我提供任何高质量的代码。以下代码是我试图解决它的方法。它不是最好的解决方案,但它可以完成工作。

try {
            InputStream is = new FileInputStream("file");
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            int[][] edges = new int[24][3];
            String line = "";
            int count = 0;
            int countpos = -1;
            while ((line = br.readLine()) != null) {
                count++;
                if(count!=1) {
                    countpos++;
                    String splitValue = line.split(",")[0];
                    String splitValue2 = line.split(",")[1];
                    String splitValue3 = line.split(",")[2];
                    edges[countpos][0] = Integer.valueOf(splitValue);
                    edges[countpos][1] = Integer.valueOf(splitValue2);
                    edges[countpos][2] = Integer.valueOf(splitValue3);

                }
            }
            System.out.println("Value read: " + countpos );
            for(int i = 0; i < edges.length; i++){
                System.out.print(edges[i][0]);
                System.out.print(edges[i][1]);
                System.out.println(edges[i][2]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }