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

标签 java arrays printing element

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

导入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);

    }

}

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

最佳答案

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

关于java - 如何打印数组列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19253202/

相关文章:

java - 在Java中执行命令julia.exe

java - 为什么我不能从 Eclipse 运行我的 Spring 项目?

jquery - 使用 JSON 在完整日历上呈现事件

Java - 返回一个整数数组

在一个 ZPL 命令中打印多个(不同的)标签

c程序打印look and say序列

java - Scala 的 loop-react 性能太差了。为什么?

c++ - 函数头中的 C/C++ 数组变量

C 中无法打印最大行

java - JAXB 和同一元素中的多种内容类型