インポートされたデータからの個々の要素と別々のアレイに分割するのArrayList

モスビー氏:

例えば、これは、プログラムに入力されています。

(A)

(!交流)

(紀元前)

例えばそれに接続されているC;!私はせずに単独で使用する必要があります。

私の考えは、ArrayListの中に格納される全てのためにした後、ブラケットの各セットは、個々の要素と異なるアレイに分割されることになります。すなわち、それは[[A]、[!、C]、[B!C]]になります。

どのような上記の言われたことは、私が念頭に置いていたという考えです。これはうまく動作しません、私が書かれていることをコードです。私は私が理解されていると言うことを試みている何を期待し、私は助けに感謝したいです

Scanner scanClauses = new Scanner(new File(args[0]));

 System.out.println("\nSuccessfully read file!\n");

 ArrayList<ArrayList<String>> clauses = new ArrayList<>();
 ArrayList<String> query = new ArrayList<>();

 Scanner scan = new Scanner(System.in);
 scan.useDelimiter("\n");

 //To take KB and put it in an array
 while (scanClauses.hasNextLine()) {
       clauses.add(new ArrayList<>());
       for (ArrayList<String> c : clauses) {
           c.add(scanClauses.next());
           System.out.println(c);
       }
 }
アービンド・クマールのAvinash:

問題は、あなたが空を追加しているということであるArrayListclausesあなたの次のコード行で:

clauses.add(new ArrayList<>());

次のように実行します。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new File("file.txt"));
        List<ArrayList<String>> clauses = new ArrayList<ArrayList<String>>();
        ArrayList<String> query;
        while (scan.hasNextLine()) {
            // Remove '(' and ')' from the line and then split it on ';' to get an array
            // of tokens. Then, convert the array to a new `ArrayList`. 
            query = new ArrayList<String>(Arrays.asList(scan.nextLine().replace("(", "").replace(")", "").split(";")));
            clauses.add(query);
        }
        System.out.println(clauses);
    }
}

出力:

[[a], [!a, c], [b, !c]]

file.txtとの内容:

(a)
(!a;c)
(b;!c)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=359937&siteId=1
おすすめ