簡體   English   中英

從文本文件創建二維數組

[英]creating 2 dimensional array from text file

我試圖從文本文件中將值添加到2d數組中。 我不知道如何將輸入文件轉換為教師的數組格式。 該文件包含進入表示頂點的數組的第一行。 那部分工作正常。 第一行之后的每一行都包含鄰接矩陣的數字。 讀取了這些數字,但由於某些原因,矩陣中的插槽未填充。 我需要使用這些數字來填充與使用此語句相同的雙精度數組:

int[][] edges = {

        {0,1}, {0,2},

        {1,0},{1,2},

        {2,0},{2,1},{2,3},{2,4},{2,6},

        {3,2},{3,4},{3,5},

        {4,2},{4,3},

        {5,3},{5,6},

        {6,2},{6,5},{6,7},

        {7,6}

我必須從txt文件讀取輸入。 名稱表示我圖中的節點或頂點的名稱。 數字表示頂點在鄰接矩陣中的位置。 例如,第一行表示映射到節點john的行o,這意味着john通過一條邊連接到peter和mary的節點1和2。 這些數字的映射方式應與上面創建數組的方式相同。 該文件如下所示:

john;peter;mary;susan;george;debbie;tom;bill;

0 1 2

1 0 2

2 0 1 3 4 6

3 2 4 5

4 2 3

5 3 6

6 2 5 7

7 6

這是我的代碼:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class TestSocialGraph {
public static void main(String args[]){
    String verts;
    int[][] edges;
    int[] intPasser;
    File input = new File("GraphInput.txt");
    String[] names;
    try {
        Scanner reader = new Scanner(input);
    verts = reader.next();
    names = verts.split(";");
    edges = new int[names.length][names.length];

    while (reader.hasNextLine()){
        int b = 0;
        int count = 0;
    String passer = reader.nextLine();
//  System.out.println(passer);
    String[] temp = passer.split(" ");
    intPasser = new int[temp.length];


    for(int i = 1; i< temp.length; i++){
        System.out.println(temp[i]);
            intPasser[b] = Integer.parseInt(temp[i]);

        b++;
        }
    System.out.println(intPasser[0]);
    for(int i = 0; i< intPasser.length; i++){

        edges[count][intPasser[i]] = 1;

    }
    count++;
    }


    } catch (FileNotFoundException e) {
        System.out.println("File not found. Please place appropriate file and restart program");
        System.exit(1);
    }
}
}

您的代碼中有很多事情需要糾正。

  • verts = reader.next(); 應該是verts = reader.nextLine();
  • 在您的問題edges20 x 2矩陣,而在您的代碼中是8 x 8正方形矩陣。 文件中邊的數量似乎並不取決於第一行中名稱的數量,那么為什么要實例化這樣的邊數組呢?
  • 變量b是無用的,您可以在循環中將其替換為i
  • 變量count永遠不會增加(或者至少永遠不會使用增加的值),因為它會在循環的每一圈重新初始化。 這就是為什么您從不填寫某些行的原因
  • 您的兩個循環可以合並為一個unic,而無需使用臨時數組

您的數據來自哪里? 我會避免自己編寫/維護解析器。 這是浪費時間。 您是否考慮過使用JSON? 您的數據(Java代碼版本)看起來完全一樣。

當您可以將數據轉換為

{
  "vertices" : ["john","peter","mary","susan","george","debbie","tom","bill"],
  "edges" : [[0,1], [0,2], ...]
}

您可以使用標准的JSON解析器讀取文檔模型並直接開始使用數據。

這是如何處理JSON數據的示例: http : //www.mkyong.com/java/json-simple-example-read-and-write-json/

看來您要建立鄰接矩陣...

即文件的第一行說

0 1 2

因此,這意味着節點0連接到節點1和節點2。它也與您的老師給出的匹配:

{0,1},{0,2},

因此,在最終的2D數組邊緣中 ,它應該像這樣表示(節點0連接到的索引中的1s):

edges [0] = {0,1,1,0,0,0,0,0};

Dici在這件事上的回答是絕對正確的-產生問題的原因是count變量的位置。 放置int count = 0; 在while循環之外,並使用readLine()而不是read(),您應該獲得該數據的鄰接矩陣表示。

唯一的問題是老師的表達方式和您要在此處嘗試表達的方式不同。 因此,隨后處理這些輸入的邏輯也將有所不同。 一個程序將使用您的老師提供的程序運行,但不會使用您創建的矩陣運行。

- - - - - - - - - - - - - -編輯 - - - - - - - - - - - -----

作為對IMO的回應,您需要兩次瀏覽文件-首先獲取邊的數量(在本例中為20),然后用實際值填充數組。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestSocialGraph {
    public static void main(String args[]) {
        int[][] edges;
        File input = new File("GraphInput.txt");
        try {
            Scanner reader = new Scanner(input);
            reader.nextLine();// Skips the first line (nameList)
            int count = 0;
            // Count the total number of 2 element arrays
            while (reader.hasNextLine()) {
                String passer = reader.nextLine();
                count += passer.split(" ").length - 1;
            }
            reader.close();

            edges = new int[count][];
            int i = 0;
            reader = new Scanner(input);
            reader.nextLine();// Skips the first line (nameList)
            while (reader.hasNextLine()) {
                String passer = reader.nextLine();
                String[] split = passer.split(" ");
                for (int j = 1; j < split.length; j++) {
                    edges[i + j - 1] = new int[2];
                    edges[i + j - 1][0] = Integer.parseInt(split[0]);
                    edges[i + j - 1][1] = Integer.parseInt(split[j]);
                }
                i += split.length - 1;
            }
            reader.close();
            print(edges);
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }

    private static void print(int[][] e) {
        for (int i = 0; i < e.length; i++) {
            for (int j = 0; j < e[i].length; j++)
                System.out.print(e[i][j] + " ");
            System.out.println();
        }
    }
}

我希望代碼不要太晦澀/不可讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM