简体   繁体   English

二维数组Java TSP

[英]Two Dimensional Array Java TSP

I need to figure out how to get the following data into two ArrayLists or arrays... 我需要弄清楚如何将以下数据分成两个ArrayList或数组...

THIS THAT OTHER

 1     2     3 
 4     5     6 
 7     8     9

I need the column titles to go into one array or ArrayList, and the integer values to go into a two dimensional array or ArrayList. 我需要将列标题放入一个数组或ArrayList中,并将整数值放入二维数组或ArrayList中。

Here is my attempt at the Strings: 这是我对弦乐的尝试:

cities = new ArrayList<String>();
String newCities;
newCities = (inFile.nextLine());
String[] stringArray = newCities.split(" ");
for (int i = 0; i < stringArray.length; i++){
    cities.add(stringArray[i]);
}
System.out.println(cities);

I can't figure out how to do the integer values. 我不知道如何做整数值。

Finally, this is for the implementation of a Traveling Salesman Problem so if you can think of a better way to handle this data, let me know! 最后,这是为实施旅行商问题而设计的,因此,如果您能想到一种更好的方式来处理此数据,请告诉我! Thanks for your time! 谢谢你的时间!

Use Scanner class instead, with its nextInt() method: 请改用Scanner类及其nextInt()方法:

Scanner scanner = new Scanner(YOUR_STRING_WITH_INTS);
while (scanner.hasNext()) {
     yourInt = scanner.nextInt()
     yourArrayList.add(yourInt);
}

and remember to close the scanner afterwards 并记得之后关闭扫描仪

scanner.close();

If you want to add this to an array, scan every line, and use an index variable to count which line it is. 如果要将其添加到数组,请扫描每一行,并使用索引变量计算它是哪一行。 You know that you have 3 integers in every line, so using your index, write to adequate "row" of the table. 您知道每行有3个整数,因此使用索引将数据写入表的足够“行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM