简体   繁体   English

如何在Java中使用ArrayList添加元素?

[英]How to add elements using ArrayList in java?

I'm now learning all about ArrayList . 我现在正在学习有关ArrayList所有知识。 I've created a small code for practice purposes using ArrayList but there has been an unwanted output. 我已经使用ArrayList创建了一个用于实践的小代码,但是有不需要的输出。

My output should be like this: 我的输出应该是这样的:

********************\\ ******************** \\

1 - Add names 1-添加名称

2 - Show names 2-显示名称

Enter mode:1 进入模式:1

Enter name:Bruno 输入名称:布鲁诺

Do you want to add a name again?(y/n):y 是否要再次添加名称?(y / n):y

Enter name:Django 输入名称:Django

Do you want to add a name again?(y/n):n 是否要再次添加名称?(y / n):n

********************\\ ******************** \\

1 - Add names 1-添加名称

2 - Show names 2-显示名称

Enter mode:2 进入模式:2

Bruno 布鲁诺

Django Django的

but instead, my output is like this: 但相反,我的输出是这样的:

********************\\ ******************** \\

1 - Add names 1-添加名称

2 - Show names 2-显示名称

Enter mode:1 进入模式:1

Enter name:Bruno 输入名称:布鲁诺

Do you want to add a name again?(y/n):y 是否要再次添加名称?(y / n):y

Enter name:Django 输入名称:Django

Do you want to add a name again?(y/n):n 是否要再次添加名称?(y / n):n

********************\\ ******************** \\

1 - Add names 1-添加名称

2 - Show names 2-显示名称

Enter mode:2 进入模式:2

When I have already added the names and then press n , I will choose next, the number 2 for displaying names. 当我已经添加了名称,然后按n时 ,接下来我将选择显示名称的数字2 But it didn't show anything. 但是它什么也没显示。 I can't figure out the algorithm. 我不知道算法。 It looks like the ArrayList is resetting its elements whenever it went back to choosing mode. 看起来只要回到选择模式, ArrayList就会重置其元素。

Here is my code: 这是我的代码:

package Practice;
import java.util.Scanner;
import java.util.ArrayList;
public class Practice1Main {

    public static void main(String[]args){
    ArrayList<String>namelist=new ArrayList<>();

    Scanner hold = new Scanner(System.in);
    String response, name;
    int mode = getMode();

    switch(mode){
    case 1:

        do{
            System.out.print("Enter name:");
            name = hold.nextLine();
            namelist.add(name);

            System.out.print("Do you want to add a name again?(y/n):");
            response = hold.nextLine();
            if(response.equalsIgnoreCase("n")){
                getMode();
            }

        }while(response.equalsIgnoreCase("y"));
        break;
    case 2:
        for(int x = 0;x < namelist.size();x++){
            System.out.println(namelist.get(x));
        }
        break;
    default:
        System.out.print("ERROR!");
        break;
    }

}



public static int getMode(){
    Scanner hold = new Scanner(System.in);

    int mode;
    System.out.print("********************\n");
    System.out.print("1 - Add names\n");
    System.out.print("2 - Show names\n");
    System.out.print("Enter mode:");
    mode = hold.nextInt();

    return mode;
}

}

Well, a quick solution is below... 好吧,下面是一个快速解决方案...

You miss setting mode when reading the getMode again, and also you do nothing when looping next time (the switch must activate again, you know.. I set it in a while loop). 您在再次读取getMode时会错过设置模式,并且在下次循环时也什么也不做(您必须再次激活该开关。我在while循环中对其进行了设置)。 study below 在下面学习

    while( mode == 1 || mode == 2 ) {
    switch(mode){
    case 1:
        do{
            System.out.print("Enter name:");
            name = hold.nextLine();
            namelist.add(name);

            System.out.print("Do you want to add a name again?(y/n):");
            response = hold.nextLine();
            if(response.equalsIgnoreCase("n")){
                mode = getMode(); // <---- set mode
            }

        }while(response.equalsIgnoreCase("y"));
        break;
    case 2:
        for(int x = 0;x < namelist.size();x++){
            System.out.println(namelist.get(x));

        }
        mode=-1; // <--- break out
        break;
    default:
        System.out.print("ERROR!");
        mode = -1; 
        break;
    }
}
}

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

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