使用ArrayList循环

时间:2015-06-22 12:47:59

标签: java arraylist do-while

我有这个代码涉及与ArrayList names有关的任务 其中包括添加ArrayList中的元素,显示ArrayList的元素,删除ArrayList中的元素,编辑ArrayList中的元素等功能。

我的问题是当我在ArrayList中添加新名称时,当代码循环回来并且我想查看ArrayList中的元素时,结果是空的。

import java.util.ArrayList;
import java.util.Scanner;

public class mainRunner {
    public static void main(String[] args) {
        String con;
        do {
            menuCaller();
            System.out.println("Do you want to continue[Y/N]"); 
            Scanner confirm = new Scanner(System.in);
            con = confirm.nextLine();
        } while (con.equalsIgnoreCase("y"));
    }

    public static void menuCaller() {
        int choice;
        ArrayList names = new ArrayList();
        int firstIndex =0;
        Scanner scan = new Scanner(System.in);
        Scanner scan2 = new Scanner(System.in);
        Scanner scaned = new Scanner(System.in);
        System.out.println("Menu"
                        +"\n[1] Add Name"
                        + "\n[2] Display All Record"
                        + "\n[3] Delete a Record"
                        + "\n[4] Edit a R1ecord "
                        + "\n[5] Exit");

        choice = scaned.nextInt();
        if (choice == 1) {
            System.out.println("***ADDING NAMES***");
            System.out.println("Enter the name of the Student");
            String addName = scan.next();
            names.add(addName);
            System.out.println("Record Added !!!");    
        }
        else if (choice == 2) {
            System.out.println("***Database Content**");
            System.out.println("------------------------");
            System.out.println("Record #   |  Name ");

            for (int index = firstIndex; index < names.size(); ++index) {
                System.out.println("--------------------");
                System.out.println("  " + index + "  |  " +names.get(index));
            }
        }
        else if (choice == 3) {
            System.out.println("***Delete A Record***");
            System.out.print("Enter a number to be deleted: ");
            int numDelete = scan.nextInt();
            names.remove(numDelete);
            System.out.println("Record Deleted");
        }
        else if (choice == 4) {
            System.out.println("***Edit Record***");
            System.out.println("***Database Content**");
            System.out.println("------------------------");
            System.out.println("Record #   |  Name ");

            for (int index = firstIndex; index < names.size(); ++index) {
                System.out.println("--------------------");
                System.out.println("  " + index + "  |  " +names.get(index));
            }

            System.out.println("Enter the Name to be Edited");
            String nameEdited = scan.next();
            if (names.indexOf(nameEdited) >= 0) {
                System.out.print("Change to: ");
                String nameChange = scan2.next();
                names.set(names.indexOf(nameEdited), nameChange);
            }
            else {
                System.out.println("Record Not Existing");
            }
        }
        else if (choice == 5) {
            System.out.println("Thank you for using the Program");
            System.exit(choice);
        }
        else {
            System.out.println("Wrong Choice");
        }
    }
}

1 个答案:

答案 0 :(得分:4)

每次拨打menuCaller时,您都会创建一个新列表。

也许您应该使用main方法创建它并将引用传递给menuCaller

public static void main(String[] args) {
    String con;
    List<String> names = new ArrayList<>();
    do{
      menuCaller(names);
      System.out.println("Do you want to continue[Y/N]"); 
      Scanner confirm = new Scanner(System.in);
      con = confirm.nextLine();    
    }while(con.equalsIgnoreCase("y"));
}

public static void menuCaller(List<String> names) {
    ...
    // (No declaration of names here - it's a parameter now...)
    ...
}