简体   繁体   中英

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?

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

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. 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. Variables within a block in Java are scoped to its bounds. In other words, no statement outside the while loop has visibility of either array. 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<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

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

to check if the given string, in your case it will be lst[i] has one or more letters. 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. 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.

For more information regarding the pattern class, you can check this tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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