簡體   English   中英

在Java中將元素添加到列表中

[英]add elements into the list in Java

這是代碼:

class Test {
    public static void main (String[] args) throws Exception {
        java.io.File fail = new java.io.File("C:/Users/Student/Desktop/Morze.txt");
        java.util.Scanner sc = new java.util.Scanner(fail);
        while (sc.hasNextLine()) {
             String line = sc.nextLine();
             String[] lst = line.split(" ");
             int[] letter = new int[26];
             int[] sumbol = new int[26];
             for (int i = 0; i < lst.length; i++)
                 System.out.print(lst[i] + " ");
             System.out.println();
                     // How to add?
        }
    }
}

請解釋如何將所有字母添加到列表中的字母和符號列入Sumbol列表?

Morze.txt文件的內容:

A .- 
B -... 
C -.-. 
D -.. 
E . 
F ..-. 
G --. 
H .... 
I .. 
J .--- 
K -.- 
L .-.. 
M -- 
N -. 
O --- 
P .--. 
Q --.- 
R .-. 
S ... 
T - 
U ..- 
V ...- 
W .-- 
X -..- 
Y -.-- 
Z --.. 

謝謝!

你沒有列表,你有一個數組。 您似乎想要將值添加到兩個數組。 但是,您的循環中似乎有一些代碼不應該在循環中。

此外,您的數據是text / String而不是numbers / int值。

String[] letter = new String[26];
String[] symbol = new String[26];
int count = 0;
while (sc.hasNextLine()) {
    String line = sc.nextLine();
    String[] lst = line.split(" ");
    letter[count] = lst[0];
    symbol[count] = lst[1];
    count++;
}
for (int i = 0; i < count; i++)
    System.out.println(letter[i] + " " + symbol[i]);

我將提供一個解決方案來修復您的實現,因為我認為它可以幫助您理解一些概念。 但是我會建議你一旦工作就回去看看Java List界面並重新編寫你的代碼。 列表是維護可能長度增長或縮小的序列的更清晰的方式,並將大大降低代碼的復雜性。

您應該首先將您的字母和符號數組聲明移出while循環。 Java中塊中的變量的范圍限定在其邊界內。 換句話說,while循環外沒有語句可以看到任何一個數組。 這具有為使用掃描儀解析的每一行創建新數組的副作用。

    int[] letter = new int[26];
    int[] sumbol = new int[26];
    while (sc.hasNextLine()) {
         String line = sc.nextLine();
         String[] lst = line.split(" ");

接下來,您需要知道將當前符號/字母放在數組中的位置,索引。 因此,您需要計算到目前為止已經處理了多少行/符號。

    int[] letter = new int[26];
    int[] sumbol = new int[26];
    int numberOfSymbolsProcessed = 0;
    while (sc.hasNextLine()) {
         String line = sc.nextLine();
         String[] lst = line.split(" ");

現在你有兩個數組和一個索引,將符號和字母添加到數組中,如下所示...

    int[] letter = new int[26];
    int[] sumbol = new int[26];
    int numberOfSymbolsProcessed = 0;
    while (sc.hasNextLine()) {
         String line = sc.nextLine();
         String[] lst = line.split(" ");
         letter[numberOfSymbolsProcessed] = lst[0];
         sumbol[numberOfSymbolsProcessed] = lst[1];
         numberOfSymbolsProcessed = numberOfSymbolsProcessed + 1;

這將是List接口的一個很好的用例。

   List<String> list = new LinkedList<String>();

   while (sc.hasNextLine()) {
         String line = sc.nextLine();
         list.addAll(Arrays.asList(line.split(" ")));
   }

如果您知道您的文件將包含字母或符號,那么您可以做的是使用Pattern類並使用正則表達式,例如

^[a-z][A-Z]+$

檢查給定的字符串,在你的情況下,它將是lst [i]有一個或多個字母。 開頭的^和結尾的$確保字符串中只有字母。

如果字符串與模式匹配,那么您知道它是一個字母,因此您可以將其添加到Letter列表中。 如果沒有,您可以將其添加到符號數據結構中。

我建議你不要使用數組,而是使用動態數據結構,例如列表的ArrayList ,因為當你向它添加元素時,它會動態增長。

有關模式類的更多信息,可以查看教程

暫無
暫無

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

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