我目前正在尝试从 TSP 文件中读取坐标,它们通常看起来像这样:
NAME: berlin52
TYPE: TSP
COMMENT: 52 locations in Berlin (Groetschel)
DIMENSION: 52
EDGE_WEIGHT_TYPE: EUC_2D
NODE_COORD_SECTION
1 565.0 575.0
2 25.0 185.0
3 345.0 750.0
4 945.0 685.0
5 845.0 655.0
6 880.0 660.0
7 25.0 230.0
8 525.0 1000.0
9 580.0 1175.0
10 650.0 1130.0
11 1605.0 620.0
12 1220.0 580.0
13 1465.0 200.0
14 1530.0 5.0
15 845.0 680.0
16 725.0 370.0
17 145.0 665.0
18 415.0 635.0
19 510.0 875.0
20 560.0 365.0
21 300.0 465.0
22 520.0 585.0
23 480.0 415.0
24 835.0 625.0
25 975.0 580.0
26 1215.0 245.0
27 1320.0 315.0
28 1250.0 400.0
29 660.0 180.0
30 410.0 250.0
31 420.0 555.0
32 575.0 665.0
33 1150.0 1160.0
34 700.0 580.0
35 685.0 595.0
36 685.0 610.0
37 770.0 610.0
38 795.0 645.0
39 720.0 635.0
40 760.0 650.0
41 475.0 960.0
42 95.0 260.0
43 875.0 920.0
44 700.0 500.0
45 555.0 815.0
46 830.0 485.0
47 1170.0 65.0
48 830.0 610.0
49 605.0 625.0
50 595.0 360.0
51 1340.0 725.0
52 1740.0 245.0
EOF
我想要做的是读取所有节点,它们的两个坐标并从中创建一个节点。我想将它们存储在一个 arraylist 存储列表中,例如:
ArrayList<String[]>
我的代码目前看起来像这样:
package group12.TSP.tree;
import java.io.File;
import java.util.*;
public class Tree {
ArrayList<String[]> storing = new ArrayList<String[]>();
public Tree() throws Exception{
File file = new File("C:/Users/joaki/Desktop/burma14.tsp");
Scanner sc = new Scanner(file);
storing = new ArrayList<String[]>();
String nextValue = null;
//sc.reset();
sc.useDelimiter(" ");
while (sc.hasNextLine()) {
sc.nextLine();
while(sc.hasNextDouble()) {
nextValue = sc.nextLine();
//st.replaceAll("\\s+","")
//nextValue = nextValue.replace(" ", "");
storing.add(nextValue.split(""));
continue;
}
}
sc.close();
}
这并没有使我想要它做的事情,但我不明白如何实现它,我想它可以只是将坐标复制到文本文件,但我希望它适用于各种 TSPS。提前致谢!
慕虎7371278
繁星淼淼
相关分类