与ArrayList一起循环(Looping with ArrayList)
我有这个代码,涉及到
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"); } } }
I have this code that involves task to do with the
ArrayList
names
that includes functions like adding elements in theArrayList
, displaying the elements of theArrayList
, deleting an element in anArrayList
, editing an element in anArrayList
.My question is when I add a new name in the
ArrayList
, when the code loops back and I want to view the elements in theArrayList
, it turns out empty.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"); } } }
原文:
更新时间:2022-08-02 19:08