[英]How to Call a Method From Another Class, Through The Main Class?
Apologies for the poor title but I don't know how to phrase my issue.为糟糕的标题道歉,但我不知道如何表达我的问题。 I am attempting to call the getSSN method from the Student class in my BubbleSorter class after a few students have been placed in an ArrayList.
在一些学生被安置在 ArrayList 之后,我试图从我的 BubbleSorter class 中的学生 class 调用 getSSN 方法。 How can I call for the specific SSN variable.
如何调用特定的 SSN 变量。 I understand why the error is occuring, but not how to change it to make it work.
我理解为什么会发生错误,但不知道如何更改它以使其正常工作。 The error message is as follows: The method getSSN() is undefined for the type SearchandSort.
错误信息如下: 对于 SearchandSort 类型,方法 getSSN() 未定义。
My Student Class:我的学生 Class:
public class Student {
private String firstName, lastName;
private int SSN;
public Student(String first, String last, int ssn) {
firstName = first;
lastName = last;
SSN = ssn;
}
public String toString() {
return lastName + ", " + firstName + " " + SSN + "\t";
}
public boolean equals(Object other) {
return (lastName.equals(((Student)other).getLastName()) && firstName.equals(((Student)other).getFirstName()) && (SSN == (((Student)other).getSSN())));
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName (String lastName) {
this.lastName = lastName;
}
public int getSSN() {
return SSN;
}
public void setSSN(int SSN) {
this.SSN = SSN;
}
}
My BubbleSorter:我的气泡分类器:
import java.lang.reflect.Array;
// “On my honor, I have neither received nor given any unauthorized assistance on this examination.”
public class BubbleSorter {
public static void bubbleSort(SortandSearch Students[]) {
int lastPos;
int index;
int temp;
for (lastPos = Students.length-1; lastPos >= 0; lastPos--) {
for (index = 0; index <= lastPos - 1; index++) {
if (((Students[index]).getSSN()) - ((Students[index+1].getSSN())) > 0) {
temp = Students[index].getSSN();
Students[index].getSSN() = Students[index+1].getSSN();
Students[index+1].getSSN() = temp;
}
}
}
}
}
My main class:我的主要 class:
import java.util.ArrayList;
import java.util.Scanner;
public class SortandSearch {
public static void main(String[] args) {
Student a = new Student("Ryan", "Jones", 123456);
Student b = new Student("Jolie", "Decker", 123457);
ArrayList<Student> Students = new ArrayList<Student>();
Students.add(a);
Students.add(b);
System.out.println(Students);
bubbleSort(Students);
System.out.println(Students);
System.out.println("Please enter the SSN of the student you are searching for:");
Scanner scan = new Scanner(System.in);
int searchKey = scan.nextInt();
for (int index = 0; index < Students.size(); index++) {
if (Students.get(index).getSSN() == searchKey) {
System.out.print("The student with SSN " + searchKey + " is located at " + index);
}
}
}
}
EDIT:编辑:
The error now shows: The type of the expression must be an array type but it resolved to ArrayList.错误现在显示:表达式的类型必须是数组类型,但它解析为 ArrayList。
New BubbleSorter:新的气泡分类器:
import java.util.ArrayList;
// “On my honor, I have neither received nor given any unauthorized assistance on this examination.”
public class BubbleSorter {
public static void bubbleSort(ArrayList<Student> studentList) {
int lastPos;
int index;
int temp;
for (lastPos = studentList.size()-1; lastPos >= 0; lastPos--) {
for (index = 0; index <= lastPos - 1; index++) {
if ((studentList.get(index).getSSN()) - ((studentList.get(index).getSSN())) > 0) {
temp = studentList[index];
studentList[index] = studentList[index+1];
studentList[index+1] = temp;
}
}
}
}
}
In SortandSearch
you put instances of Student
into ArrayList<>, and the your bubbleSort
method must also take argument of this type or at least List
and then convert it to array and return sorted list:在
SortandSearch
,您将Student
的实例放入 ArrayList<> 中,并且您的bubbleSort
方法还必须采用此类型或至少List
的参数,然后将其转换为数组并返回排序列表:
public static List<Student> bubbleSort(List<Student> studentsList) {
Student[] Students = studentsList.toArray(new Student[studentsList.size()]);
// ...
return Arrays.asList(Students);
}
And in the calling main method you need to store sorted list:在调用 main 方法中,您需要存储排序列表:
List<Student> sorted = BubbleSorter.bubbleSort(Students);
System.out.println(sorted);
There seem multiple things wrong to this code.这段代码似乎有很多问题。 I think you are trying unnessecary casting your arraylist to something else
我认为您正在尝试将 arraylist 转换为其他东西
First, I think your function signature should change to首先,我认为您的 function 签名应该更改为
public static void bubbleSort(ArrayList<Student> studentList)
And then you can access the elements with:然后您可以通过以下方式访问元素:
studentList.get(index).getSSN()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.