Java – 从数组(类)中查找特定的ID

huangapple go评论68阅读模式
英文:

Java - Find specific ID from an array(classes)

问题

以下是翻译好的部分:

private static boolean findStudentId() {
    System.out.println("请输入您的学生ID以搜索您的个人资料");
    int searchId = scanner.nextInt();
    // 需要能够通过搜索他们的ID从ArrayList中找到学生的个人资料
    return false;
}

我正在尝试从另一个类调用一个方法,但无法做到。这是我试图调用的另一个类:

public boolean findStudent(int id) {
    for (int i = 0; i < students.size(); i++) {
        if (students.equals(id)) {
            System.out.println("找到包含信息的个人资料:" + id);
            // System.out.println(id.getFirstName()+id.getFirstName()+id.getLastName()+id.getDob());
            return true;
        } else
            System.out.println("无法根据提供的ID找到个人资料");
    }
    return false;
}

这是我创建的表示学生的学生类:

public class Student {
    private int id;
    private String firstName;
    private String lastName;
    private String dob;

    public Student(int id, String firstName, String lastName, String dob) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getDob() {
        return dob;
    }

    public static Student createStudentID(int id, String firstName, String lastName, String dob) {
        return new Student(id, firstName, lastName, dob);
    }
}

请注意,翻译后的代码片段中可能仍存在一些格式或细微错误,您可以进行适当的调整。

英文:

Having some problems trying to call a method from a class.
Have my main method below

private static boolean findStudentId() {
        System.out.println(&quot;Please enter your student ID to search for your profile&quot;);
        int searchId = scanner.nextInt();
        //need to be able to find a student&#39;s profile from the arraylist by searching their ID
        return false;
    }

I am trying to call a method from another class and cannot do so. this is my other class I am trying to call

public boolean findStudent(int id) {
        for(int i = 0; i&lt; students.size();i++)
        {
            if (students.equals(id)) {
                System.out.println(&quot;Found the profile containing information for &quot; + id);
//                System.out.println(id.getFirstName()+id.getFirstName()+id.getLastName()+id.getDob());
                return true;
            } else
                System.out.println(&quot;Could not find a profile based on the ID you provided&quot;);
        }
        return false;
    }


this is my student class that I have created that proclaims the students

public class Student {
    private int id;
    private String firstName;
    private String lastName;
    private String dob;

    public Student(int id,String firstName, String lastName, String dob) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.dob = dob;
    }

    public int getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getDob() {
        return dob;
    }

    public static Student createStudentID(int id,String firstName, String lastName, String dob)
    {
        return new Student(id,firstName, lastName, dob);
    }
}


答案1

得分: 1

如果您想从另一个类中调用一个方法,例如您可以创建一个属于您要获取方法的类的对象。例如...

如果您拥有的方法位于A类内部,则创建该类的对象。

A classAttribute = new A();

然后要调用该方法,请使用...

classAttribute.someMethod();
英文:

If you want to call a method from another class you can for example create an object of the class you are trying to get the method from. For instance...

If the method you have is located inside of Class A, then create an object of that class.

A classAttribute = new A();

Then to call the method use....

classAttribite.someMethod();

答案2

得分: 1

我建议添加一个新类,将您将要使用的所有方法放入该类中,然后在主类中创建一个对象,您可以轻松地使用那些方法,毫无问题。

+小提示:findStudent方法中的条件应该是这样的:

if (students.get(i).id == id)

这样它就会在数组列表中搜索该id。

英文:

I suggest to add a new class and put all the methods that you are going to be using in that class then in the main class create an object and you could easily use that methods with no problem

+small note the condition in findStudent method should be like

if (students.get(i).id == id)

so it would search for the id in the arraylist

huangapple
  • 本文由 发表于 2020年8月30日 03:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定