gpt4 book ai didi

java - 如何迭代、搜索和显示 ArrayList 中的特定元素

转载 作者:行者123 更新时间:2023-12-01 22:44:39 25 4
gpt4 key购买 nike

我在程序的特定部分遇到问题。我想做的是迭代、搜索并显示 ArrayList 中的元素。

所以我尝试将自己的代码片段实现到代码的主要函数中来尝试:

else if (menuChoice==3) {
System.out.println("Search Student:");
String search = input.nextLine();

for (Student student : students)
{
if (students.equals(search)){
System.out.println(student);
}
}
}

希望它能够迭代 ArrayList 并访问/​​显示列表中的特定元素。它返回空白,我做错了什么?

如果您想知道,这里是完整代码:

package student;
import java.util.*;

public class Student
{
private String r_name;
private int r_age;
private String r_course;
private String r_year;
private String r_section;
private String r_studno;

public Student( String name, int age, String course, String year, String section, String studno )
{
r_name = name;
r_age = age;
r_course = course;
r_year = year;
r_section = section;
r_studno = studno;
}

public String getName()
{
return r_name;
}

public int getAge()
{
return r_age;
}

public String getCourse()
{
return r_course;
}

public String getYear()
{
return r_year;
}

public String getSection()
{
return r_section;
}

public String getStudno()
{
return r_studno;
}

public String toString()
{
return "Name: " + r_name + ", Age: " + r_age +
", Course: " + r_course + ", Year: " + r_year +
", Section: " + r_section + ", Student Number: " + r_studno;
}

public static void main(String[] args)
{
ArrayList<Student> students = new ArrayList<Student>();
Scanner input = new Scanner(System.in);

int menuChoice = 4;
do {
System.out.println("\t\t\tStudent Record Menu");
System.out.println("\t\t1. Add Student\t2. View Students\t3. Search Student\t4. Exit");
try {
System.out.println("Enter a choice: ");
menuChoice = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
continue;
}

if (menuChoice==1)
{
System.out.println("Add a Student");
System.out.println("Full name:");
String name = input.nextLine();

int age = -1;
do {
try {
System.out.println("Age:");
age = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
System.out.println("Enter a number!");
continue;
}
} while (age <= 0);

System.out.println("Course:");
String course = input.nextLine();

System.out.println("Year:");
String year = input.nextLine();

System.out.println("Section:");
String section = input.nextLine();

System.out.println("Student Number:");
String studno = input.next();

Student student = new Student(name, age, course, year, section, studno);
students.add(student);

} else if (menuChoice==2) {
System.out.println("Students:");
for (Student student : students)
{
System.out.println(student);
}
} else if (menuChoice==3) {
System.out.println("Search Student:");
String search = input.nextLine();

for (Student student : students)
{
if (students.equals(search)){
System.out.println(student);
}
}
}

} while (menuChoice<4);
}
}

最佳答案

您正在检查 ArrayList students 是否等于 String search。结果只能是假的。我猜,您正在尝试执行以下操作:

for (Student student : students)
{
if (student.getName().equals(search))
{
System.out.println(student);
break;//assuming student name are unique. remove if not
}
}

关于java - 如何迭代、搜索和显示 ArrayList 中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25546141/

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