简体   繁体   English

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

[英]How to iterate, search and display a specific element in an ArrayList

I'm having a problem with a specific part of a program. 我对程序的特定部分有疑问。 What I want to do is iterate, search and display an element from an ArrayList. 我想做的是迭代,搜索和显示ArrayList中的元素。

So I tried implementing my own snippet to the main function of the code to try: 因此,我尝试对代码的主要功能实现自己的代码段以进行尝试:

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);
               }
           }
       }

Hoping that it would iterate through the ArrayList and access/display that specific element in the list. 希望它可以遍历ArrayList并访问/显示列表中的特定元素。 It returns blank, what am I doing wrong? 它返回空白,我在做什么错?

Here's the whole code if you're wondering: 如果您想知道,这是完整的代码:

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);
}
}

You are checking if the ArrayList students is equal to the String search . 您正在检查ArrayList students是否等于String search The result can only be false. 结果只能是假的。 I guess, you are trying to do the following: 我想,您正在尝试执行以下操作:

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

You must compare the search key with the key id of student for example if all names are unique then use 您必须将搜索键与学生的键ID进行比较,例如,如果所有名称都是唯一的,则使用

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

You are compairing the whole student object reference with the search key how it will compare it 您正在使用搜索键对整个学生对象参考进行比较

if you want to display the whole content then you will have to make a method in which you will get all details using the id eg: 如果您想显示全部内容,那么您将必须采用一种方法,使用id获得所有详细信息,例如:

getStudentbyname( String studentname){

here comes the code to get all data

}

then in your for loop call the method and store in array 然后在for循环中调用方法并存储在数组中

 for (Student student : students)
       {
           if (student.getname().equals(search)){
              Arraylist<String> studentarr = student.getStudentbyname(student.getname());

System.out.println(""+studentarr)
           }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM