Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

java.util.scanner - Java - read file with Scanner to create non-duplicate objects

I have a graph implementation where I can create a graph by doing the following:

Graph<Integer, String> g = new AdjacencyList<>(false);
Vertex<Integer> start = new Vertex<>(1);
Vertex<Integer> two = new Vertex<>(2);
Vertex<Integer> three = new Vertex<>(3);
Vertex<Integer> four = new Vertex<>(4);
Vertex<Integer> five = new Vertex<>(5);
Vertex<Integer> six = new Vertex<>(6);
g.addEdge(start, two, null, null);
g.addEdge(start, three, null, null);
g.addEdge(two, six, null, null);
g.addEdge(three, four, null, null);
g.addEdge(three, six, null, null);
g.addEdge(five, six, null, null);

This would output this adjacency list:

1: [{source: 1, dest: 2}, {source: 1, dest: 3}]
2: [{source: 2, dest: 1}, {source: 2, dest: 6}]
3: [{source: 3, dest: 1}, {source: 3, dest: 4}, {source: 3, dest: 6}]
4: [{source: 4, dest: 3}]
5: [{source: 5, dest: 6}]
6: [{source: 6, dest: 2}, {source: 6, dest: 3}, {source: 6, dest: 5}]

It would be cumbersome to build out larger graphs so I wanted to use Scanner to read graph data from text files. The file for the graph above would look like the following with the vertices in the first line and the edges in the following lines.

1 2 3 4 5 6
1, 2
1, 3
2, 6
3, 4
3, 6
5, 6

My Scanner attempt runs into reference semantics issues where it adds duplicate edges to vertices. I tried to replicate the manual process I have above but wasn't sure how to bind new vertex objects to variables. Is there a better way to read from files to create multiple objects of different types?

Scanner scan = new Scanner(new File("starter.txt"));
String firstLine = scan.nextLine();
String[] vs = firstLine.split(" ");
ArrayList<Vertex<Integer>> data = new ArrayList<>();
for (int i = 0; i < vs.length; i++) {
    data.add(new Vertex<Integer>(Integer.parseInt(vs[i])));
}
while (scan.hasNextLine()) {
    String edges = scan.nextLine();
    String[] es = edges.split(", ");
    Integer u = Integer.parseInt(es[0]);
    Integer v = Integer.parseInt(es[1]);
    g.addEdge(data.get(u - 1), data.get(v - 1), null, null);
}
scan.close();
1: [{source: 1, dest: 2}, {source: 1, dest: 3}, {source: 1, dest: 2}, {source: 1, dest: 3}]
2: [{source: 2, dest: 1}, {source: 2, dest: 6}, {source: 2, dest: 1}, {source: 2, dest: 6}]
3: [{source: 3, dest: 1}, {source: 3, dest: 4}, {source: 3, dest: 6}, {source: 3, dest: 1}, {source: 3, dest: 4}, {source: 3, dest: 6}]
4: [{source: 4, dest: 3}, {source: 4, dest: 3}]
5: [{source: 5, dest: 6}, {source: 5, dest: 6}]
6: [{source: 6, dest: 2}, {source: 6, dest: 3}, {source: 6, dest: 5}, {source: 6, dest: 2}, {source: 6, dest: 3}, {source: 6, dest: 5}]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...