使用 ArrayList 循环
Looping with ArrayList
我有这段代码涉及 ArrayList
names
的任务
其中包括在 [=11= 中添加元素、显示 [=11= 中的元素]、删除 [=11= 中的元素]、编辑 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");
}
}
}
每次调用 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...)
...
}
我有这段代码涉及 ArrayList
names
的任务
其中包括在 [=11= 中添加元素、显示 [=11= 中的元素]、删除 [=11= 中的元素]、编辑 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");
}
}
}
每次调用 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...)
...
}