亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

掃描儀出現問題,無法讀取 TSP 文件

掃描儀出現問題,無法讀取 TSP 文件

慕森王 2021-10-28 14:14:57
我目前正在嘗試從 TSP 文件中讀取坐標,它們通??雌饋硐襁@樣:NAME: berlin52TYPE: TSPCOMMENT: 52 locations in Berlin (Groetschel)DIMENSION: 52EDGE_WEIGHT_TYPE: EUC_2DNODE_COORD_SECTION1 565.0 575.02 25.0 185.03 345.0 750.04 945.0 685.05 845.0 655.06 880.0 660.07 25.0 230.08 525.0 1000.09 580.0 1175.010 650.0 1130.011 1605.0 620.0 12 1220.0 580.013 1465.0 200.014 1530.0 5.015 845.0 680.016 725.0 370.017 145.0 665.018 415.0 635.019 510.0 875.0  20 560.0 365.021 300.0 465.022 520.0 585.023 480.0 415.024 835.0 625.025 975.0 580.026 1215.0 245.027 1320.0 315.028 1250.0 400.029 660.0 180.030 410.0 250.031 420.0 555.032 575.0 665.033 1150.0 1160.034 700.0 580.035 685.0 595.036 685.0 610.037 770.0 610.038 795.0 645.039 720.0 635.040 760.0 650.041 475.0 960.042 95.0 260.043 875.0 920.044 700.0 500.045 555.0 815.046 830.0 485.047 1170.0 65.048 830.0 610.049 605.0 625.050 595.0 360.051 1340.0 725.052 1740.0 245.0EOF我想要做的是讀取所有節點,它們的兩個坐標并從中創建一個節點。我想將它們存儲在一個 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。提前致謝!
查看完整描述

2 回答

?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

在這里做了一些改變。我讀到“NODE_COORD_SECION”然后開始解析存儲行的ans。我不是在“”上拆分,而是在“”上拆分并存儲值。


public class Tree {

    ArrayList<String[]> storing;


    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;

        while (sc.hasNextLine()) {

            String line = sc.nextLine();

            if("NODE_COORD_SECTION".equals(line)){

                while (sc.hasNextLine()) {

                    nextValue = sc.nextLine();

                    storing.add(nextValue.trim().split(" "));

                }

            }

        }

        sc.close();

    }


    public static ArrayList<String[]> returnScanner() throws Exception {

        Tree tree = new Tree();

        return tree.storing;

    }


    public static void main(String[] args) throws Exception {

        ArrayList<String[]> storedValues = returnScanner();

        String[] firstLine = storedValues.get(0);

        String[] secondLine = storedValues.get(1);

        for (int i = 0; i < firstLine.length; i++) {

            System.out.println(firstLine[i]);

        }

    }

}

我的輸出:


1

565.0

575.0


查看完整回答
反對 回復 2021-10-28
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

使用掃描儀移動到下一行,直到遇到短語“NODE_COORD_SECTION”。然后接下來的幾行是你的數據線。它們都符合格式,因此您可以使用 split 來獲取第二個和第三個元素。

當您到達標有“EOF”的行時,停止讀取和存儲在您的數組中。

你有多關心 TSP 文件的標題?如果要存儲此信息并根據文件中的數據檢查它是否正確,而不是僅運行到“NODE_COORD_SECTION”行,則需要查找行:“DIMENSION”并將值存儲為 int。然后根據您的 ArrayList“存儲”中的最終總數檢查此值


查看完整回答
反對 回復 2021-10-28
  • 2 回答
  • 0 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號