Java 如何打印数组列表中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19253202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:30:09  来源:igfitidea点击:

How to print elements from an array list

javaarraysprintingelement

提问by Fuzail Gilani

I've looked online for help so far and nothing is working for me. This is what I have right now, can you please tell me what is wrong with it?

到目前为止,我一直在网上寻求帮助,但对我没有任何帮助。这就是我现在所拥有的,你能告诉我它有什么问题吗?

import java.util.*;

导入 java.util.*;

class employeeProgram{

    public static void main(String[] args){

            for (int count = 0; count < 1; ){

                    Scanner input = new Scanner(System.in);

                    ArrayList<String> employeeNames = new ArrayList<String>();

                    String commnd;

                    print("What would you like to do? (Add, Remove, List, Exit): ");

                    commnd = input.next();

                    if ("Add".equals(commnd)){

                            print("Enter the name of the employee you'd like to add to the list: ");
                            employeeNames.add(input.next());


                    } else if ("Remove".equals(commnd)){

                            print("Enter the name of the employee you'd like to remove from the list: ");

                            employeeNames.remove(input.next());

                    } else if ("List".equals(commnd)){

                            for (int j = 0; j < employeeNames.size(); j++){

                                    println(employeeNames.get(j));
                                    println(j);

                            }


                            //println(employeeNames);

                    } else if ("Exit".equals(commnd)){

                            input.close();
                            break;

                    } else {

                            println("Invalid command.");

                    }
            }
    }
    public static void println(Object x){

            System.out.println(x);

    }
    public static void print(Object x){

            System.out.print(x);

    }

}

The for loop isn't beginning, I know that much because the "println(j)" wasn't working either. When I tried the part that I've commented out, all I got from typing list was "[]" and nothing else. I'm a beginner, and this is my first program using ArrayLists, so thanks in advance to anyone who decides to help me out :)

for 循环还没有开始,我知道很多是因为“println(j)”也没有工作。当我尝试我注释掉的部分时,我从输入列表中得到的只是“[]”而没有别的。我是一个初学者,这是我第一个使用 ArrayLists 的程序,所以提前感谢任何决定帮助我的人:)

回答by Juned Ahsan

Define your employeeNamesList outside the first forloop. ArrayList is getting re-intialized with every iteration of the loop.

employeeNames在第一个for循环之外定义您的列表。ArrayList 会随着循环的每次迭代重新初始化。

回答by Hello_Everyone

for (int count = 0; count < 1; count++)

for (int count = 0; count < 1; count++)

回答by Husman

You need to define your arraylist outsode of your loop, otherwise every iteration creates a new arrayList and the previous changes made to the previous arraylist is garbage collected.

您需要在循环外定义 arraylist,否则每次迭代都会创建一个新的 arrayList,并且之前对之前的 arraylist 所做的更改将被垃圾收集。

import java.util.*;

class employeeProgram{

    public static void main(String[] args){

            ArrayList<String> employeeNames = new ArrayList<String>();

            for (int count = 0; count < 1; ){

                    Scanner input = new Scanner(System.in);



                    String commnd;

                    print("What would you like to do? (Add, Remove, List, Exit): ");

                    commnd = input.next();

                    if ("Add".equals(commnd)){

                            print("Enter the name of the employee you'd like to add to the list: ");
                            employeeNames.add(input.next());


                    } else if ("Remove".equals(commnd)){

                            print("Enter the name of the employee you'd like to remove from the list: ");

                            employeeNames.remove(input.next());

                    } else if ("List".equals(commnd)){

                            for (int j = 0; j < employeeNames.size(); j++){

                                    println(employeeNames.get(j));
                                    println(j);

                            }


                            //println(employeeNames);

                    } else if ("Exit".equals(commnd)){

                            input.close();
                            break;

                    } else {

                            println("Invalid command.");

                    }
            }
    }
    public static void println(Object x){

            System.out.println(x);

    }
    public static void print(Object x){

            System.out.print(x);

    }

}

Also, there are a few basic changes I would make to this program, first of all - a for loop should be used where you know how many iterations you will do. In your case, a while loop (or better still a do-while loop) is better suited.

另外,我会对这个程序做一些基本的改变,首先 - 应该使用 for 循环,在那里你知道你将进行多少次迭代。在您的情况下,while 循环(或更好的 do-while 循环)更适合。

Finally creating one liner functions to encapsulate system.out.print and println seem more effort than is necessary.

最后创建一个 liner 函数来封装 system.out.print 和 println 似乎比必要的工作要多。

回答by tom

You are creating a new ArrayList everytime you ask a question. Create it outside of the initial loop.

每次您提出问题时,您都在创建一个新的 ArrayList。在初始循环之外创建它。

ArrayList<String> employeeNames = new ArrayList<String>();

回答by henderso

I would try something else instead of the for loop:

我会尝试其他方法而不是 for 循环:

Although what you have here might technically work, for...loops are for deterministic processes. Here you are using it as an infinite loop that you hope to force break at a later point. This is dangerous and could lead to bugs.

尽管您在这里的内容在技术上可能有效,但 for... 循环适用于确定性过程。在这里,您将其用作无限循环,希望稍后强制中断。这是危险的,可能会导致错误。

I'd change this:

我会改变这个:

for (int count = 0; count < 1; ){

to this:

对此:

 bool done = false;
 while(!done) {

Then, later, when you want to break out. issue this line:

然后,稍后,当您想突破时。发出这一行:

 done = true;