Java ArrayList问题

R4n*_*4sk 0 java arrays variables reference arraylist

我试图允许"Admin"类创建多个数组.但是,每次运行此循环时,它似乎都会删除先前创建的数组.

   do { 
                System.out.println("\nWhat do you want "
                 + "the name of your line to be?: ");

              String lineName= keyboard1.nextLine();


   System.out.println("\n\nCongratulations! \n"+ "Your "+ lineName +
          " queue has been created! \n");


       //Create an array that will hold "User" information.
        ArrayList<User>queue=new ArrayList<>();


        //Create an array of arrays to allow "User" to see all "lines" available to them.
        ArrayList<String>totalLines=new ArrayList<>();
            `//Add the array name to the array of arrays.     
               `totalLines.add(lineName);
        //Show Admin all of their lines.
         System.out.println("Here is a list of all of your lines: \n");
       for (String s:totalLines)
       {

           System.out.println(s);

       }

             //Ask admin if they want to create another line.
             System.out.println("\nDo you wish to add another line?\n"
                + "\nType:Yes or No?");

                 y = keyboard1.nextLine();
      } while ("Yes".equals(y)||"yes".equals(y));
Run Code Online (Sandbox Code Playgroud)

我正在学习Java,我正在努力...仍然不熟悉Java中的许多工具,因为到目前为止我们只介绍了基础知识,但我很确定我的问题是变量"lineName"正在编写在之前创建的ArrayList实例上.我有事吗?

shm*_*sel 5

ArrayList在每一轮实例化一个新的.你需要在循环之外完成它:

ArrayList<String>totalLines=new ArrayList<>();
do {
//...
Run Code Online (Sandbox Code Playgroud)