gpt4 book ai didi

java - 使用 ArrayList 循环

转载 作者:行者123 更新时间:2023-12-01 18:13:46 26 4
gpt4 key购买 nike

我有这段代码涉及与 ArrayList names 相关的任务包括在 ArrayList 中添加元素、显示 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");
}
}
}

最佳答案

每次调用 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...)
...
}

关于java - 使用 ArrayList 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30980403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com