简体   繁体   English

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

[英]add elements into the list in Java

Here is the code: 这是代码:

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?
        }
    }
}

Please, explain how can I add all letters into list Letter and symbols into list Sumbol? 请解释如何将所有字母添加到列表中的字母和符号列入Sumbol列表?

Content of the file Morze.txt: 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 --.. 

Thanks! 谢谢!

You don't have a list, you have an array(s). 你没有列表,你有一个数组。 It appears you want to add the values to two arrays. 您似乎想要将值添加到两个数组。 However you appear to have some code in your loop which should not be in your loop. 但是,您的循环中似乎有一些代码不应该在循环中。

Additionally your data is text/String not numbers/int values. 此外,您的数据是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]);

I'm going to offer a solution that fixes your implementation because I think it might help you understand a few concepts. 我将提供一个解决方案来修复您的实现,因为我认为它可以帮助您理解一些概念。 However I would recommend once you get it working that you go back and read about the Java List interface and re-write your code. 但是我会建议你一旦工作就回去看看Java List界面并重新编写你的代码。 Lists are much cleaner way of maintaing sequences that may grow or shrink in length and will greatly reduce the complexity of your code. 列表是维护可能长度增长或缩小的序列的更清晰的方式,并将大大降低代码的复杂性。

You should start by moving your letter and symbol array declarations out of your while loop. 您应该首先将您的字母和符号数组声明移出while循环。 Variables within a block in Java are scoped to its bounds. Java中块中的变量的范围限定在其边界内。 In other words, no statement outside the while loop has visibility of either array. 换句话说,while循环外没有语句可以看到任何一个数组。 This has the side-effect of creating a new array for every line you parse using your scanner. 这具有为使用扫描仪解析的每一行创建新数组的副作用。

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

Next you'll need to know where to put your current symbol/letter in the array, an index. 接下来,您需要知道将当前符号/字母放在数组中的位置,索引。 So you'll want to keep a count of how many lines/symbols you've processed so far. 因此,您需要计算到目前为止已经处理了多少行/符号。

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

Now you have two arrays and an index into each, add the symbol and letter to the array as follows... 现在你有两个数组和一个索引,将符号和字母添加到数组中,如下所示...

    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;

This would be an excellent usecase for the List interface. 这将是List接口的一个很好的用例。

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

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

If you know that your file will either have letters or symbols, then, what you can do is to use the Pattern class and use a regular expression such as 如果您知道您的文件将包含字母或符号,那么您可以做的是使用Pattern类并使用正则表达式,例如

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

to check if the given string, in your case it will be lst[i] has one or more letters. 检查给定的字符串,在你的情况下,它将是lst [i]有一个或多个字母。 The ^ at the beginning and $ at the end ensure that you have only letters in the string. 开头的^和结尾的$确保字符串中只有字母。

If the string matches the pattern, than you know that it is a letter, so you can add it to the Letter list. 如果字符串与模式匹配,那么您知道它是一个字母,因此您可以将其添加到Letter列表中。 If it does not, you can add it to the symbol data structure. 如果没有,您可以将其添加到符号数据结构中。

I recommend that you do not use arrays, but rather dynamic data structures such as an ArrayList for your lists since this will grow dynamically as you add elements to it. 我建议你不要使用数组,而是使用动态数据结构,例如列表的ArrayList ,因为当你向它添加元素时,它会动态增长。

For more information regarding the pattern class, you can check this tutorial 有关模式类的更多信息,可以查看教程

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

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