引用Referenceclass中的数组变量,用另一种方法排序,在case语句中调用排序后的值
Referencing the array variables in the Reference class, sorting it using another method, and invoking the sorted values in the case statement
我正在尝试调用引用 class 中的数组变量,尝试使用用户定义的方法对它们进行排序,并将该方法调用到 case 语句上,如果用户选择特定的语句将调用该语句数字。我想为用户提供选项,学生的哪些属性将被排序(即姓名、课程...)并显示在 case 语句中调用并通过 main 方法调用的排序后的一维数组。
参考文献class中的变量:
class RecordReference {
private int idNumber;
private String firstName = "";
private String middleName = "";
private String lastName = "";
private int age;
private String yearLevel;
private String course = "";
private double gwa;
public RecordReference(int i, String f, String m, String l, int a, String y, String c, double g) {
idNumber = i;
firstName = f;
middleName = m;
lastName = l;
age = a;
yearLevel = y;
course = c;
gwa = g;
}
public int getIdNumber() {
return idNumber;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getYearLevel() {
return yearLevel;
}
public String getCourse() {
return course;
}
public double getGwa() {
return gwa;
}
public void setIdNumber(int idnumber) {
idNumber = idnumber;
}
public void setFirstName(String fName) {
firstName = fName;
}
public void setMiddleName(String mName) {
middleName= mName;
}
public void setLastNameName(String lName) {
lastName= lName;
}
public void setAge(int a) {
age = a;
}
public void setYearLevel(String yLevel) {
yearLevel = yLevel;
}
public void setCourse(String c) {
course = c;
}
public void setGwa(int gwa) {
gwa = gwa;
}
public String toString() {
return String.valueOf(System.out.printf("%-15s%-15s%-15d%-15d%n",
firstName, course , yearLevel ,gwa));
}
} // class
结束
而且我正在尝试在这种排序方法中调用它,但我不知道如何引用它。
public static void sortFirstNameArray(String[] f){
for (int i = 0; i < f.length - 1; i++) {
for (int j = i + 1; j < f.length; j++) {
if (f[i].compareToIgnoreCase(f[j]) > 0) {
String temp = f[i];
f[i] = f[j];
f[j] = temp;
}
}
}
}
排序成功完成后,我将在用户选择特定数字后调用的 switch case 语句中调用它。这部分有 5 个案例陈述(姓名、年龄、课程、一般加权平均数和对所有内容进行排序的选项 - 如果可行,我计划添加更多学生属性)
(我不知道我是否应该将它存储在另一个方法中并在 main 方法中调用它,或者只是将它放在 main 方法中)
public RecordReference Process(RecordReference[] f, RecordReference[] a) {
// for loop?
for (int x = 0; x < f.length; x++) {
switch (choice) {
case 1:
System.out.println("Sorted array of first name: ");
sortFirstNameArray(f[x].getFirstName());
System.out.printf("%-15s%n", Arrays.toString(f));
break;
case 2:
System.out.println("Sorted array of age: ");
// invokes the age method
sortAgeArray(a[x].getAge());
System.out.printf("%-15s%n", Arrays.toString(a));
break;
}
}
}
如果是在另外一个方法中,那么在main方法中调用的时候要包含什么参数呢?
我试过了,但是没用,我不知道该怎么办
System.out.print("Please choose what student attribute you want to
sort :");
choice = keyboard.nextInt();
// calling the process method here, but I receive syntax error
Process(f,a); // Here, I want to pass the sorted values back into the array but I get an error.
如果你能帮助我,那就太好了。提前谢谢你。
我只是一年级学生,我很想学习如何解决这个错误。
很高兴看到您自己尝试了这个问题并更正了您的问题以使其更清楚,因此我愿意提供帮助。
我尽量使问题的解决方案与您的解决方案尽可能接近,以便您能够理解它。可能有更好的方法来解决这个问题,但这不是这里的重点。
首先,让我们创建一个名为 BubbleSorter
的 class 来存放排序方法:
public class BubbleSorter
{
//Explicitly provide an empty constructor for good practice.
public BubbleSorter(){}
//Method that accepts a variable of type RecordReference[], sorts the
//Array based on the firstName variable within each RecordReference
//and returns a sorted RecordReference[].
public RecordReference[] SortByFirstName(RecordReference[] recordReferencesList)
{
for (int i = 0; i < recordReferencesList.length - 1; i++) {
for (int j = i + 1; j < recordReferencesList.length; j++) {
if (recordReferencesList[i].getFirstName().compareToIgnoreCase
(recordReferencesList[j].getFirstName()) > 0) {
RecordReference temp = recordReferencesList[i];
recordReferencesList[i] = recordReferencesList[j];
recordReferencesList[j] = temp;
}
}
}
return recordReferencesList;
}
}
这给了我们一个可以实例化的class,可以在其中添加方法用于排序。我添加了其中一种方法,该方法采用 RecordReference[]
作为参数,并根据每个 RecordReference
中的 firstName
class 变量对 RecordReference[]
进行排序。您将需要添加更多自己的方法来对其他 class 变量进行排序。
现在主要 class:
class Main {
public static void main(String[] args) {
//Get a mock array from the GetMockArray() function.
RecordReference[] refArray = GetMockArray();
//Instantiate an instance of BubbleSorter.
BubbleSorter sorter = new BubbleSorter();
//Invoke the SortByFirstName method contained within the BubbleSorter
//and store the sorted array in a variable of type RecordReference[] named
//sortedResult.
RecordReference[] sortedResult = sorter.SortByFirstName(refArray);
//Print out the results in the sorted array to check if they are in the correct
//order.
//This for loop is not required and is just so that we can see within the
//console what order the objects in the sortedResult are in.
for(int i = 0; i < sortedResult.length; i++)
{
System.out.println(sortedResult[i].getFirstName());
}
}
public static RecordReference[] GetMockArray()
{
//Instantiate a few RecordReferences with a different parameter for
//the firstName in each reference.
RecordReference ref1 = new RecordReference(0, "Ada", "Test", "Test", 22, "First",
"Computer Science", 1.0f);
RecordReference ref2 = new RecordReference(0, "Bob", "Test", "Test", 22, "First",
"Computer Science", 1.0f);
RecordReference ref3 = new RecordReference(0, "David", "Test", "Test", 22,
"First", "Computer Science", 1.0f);
//Create a variable of type RecordReference[] and add the RecordReferences
//Instantiated above in the wrong order alphabetically (Based on their firstName)
//class variables.
RecordReference[] refArray = {
ref2, ref3, ref1
};
return refArray;
}
}
在主要 class 中,我提供了详细的评论来准确解释正在发生的事情。我想指出的一件事是我添加了一个名为 GetMockArray()
的方法。这只是为了提供 RecordReference[]
进行测试,您可能想在您选择的其他地方进行测试。
如果有任何不清楚的地方或您需要更多帮助,请对此答案发表评论,我会尽力帮助您。
谢谢。
我正在尝试调用引用 class 中的数组变量,尝试使用用户定义的方法对它们进行排序,并将该方法调用到 case 语句上,如果用户选择特定的语句将调用该语句数字。我想为用户提供选项,学生的哪些属性将被排序(即姓名、课程...)并显示在 case 语句中调用并通过 main 方法调用的排序后的一维数组。
参考文献class中的变量:
class RecordReference {
private int idNumber;
private String firstName = "";
private String middleName = "";
private String lastName = "";
private int age;
private String yearLevel;
private String course = "";
private double gwa;
public RecordReference(int i, String f, String m, String l, int a, String y, String c, double g) {
idNumber = i;
firstName = f;
middleName = m;
lastName = l;
age = a;
yearLevel = y;
course = c;
gwa = g;
}
public int getIdNumber() {
return idNumber;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getYearLevel() {
return yearLevel;
}
public String getCourse() {
return course;
}
public double getGwa() {
return gwa;
}
public void setIdNumber(int idnumber) {
idNumber = idnumber;
}
public void setFirstName(String fName) {
firstName = fName;
}
public void setMiddleName(String mName) {
middleName= mName;
}
public void setLastNameName(String lName) {
lastName= lName;
}
public void setAge(int a) {
age = a;
}
public void setYearLevel(String yLevel) {
yearLevel = yLevel;
}
public void setCourse(String c) {
course = c;
}
public void setGwa(int gwa) {
gwa = gwa;
}
public String toString() {
return String.valueOf(System.out.printf("%-15s%-15s%-15d%-15d%n",
firstName, course , yearLevel ,gwa));
}
} // class
结束而且我正在尝试在这种排序方法中调用它,但我不知道如何引用它。
public static void sortFirstNameArray(String[] f){
for (int i = 0; i < f.length - 1; i++) {
for (int j = i + 1; j < f.length; j++) {
if (f[i].compareToIgnoreCase(f[j]) > 0) {
String temp = f[i];
f[i] = f[j];
f[j] = temp;
}
}
}
}
排序成功完成后,我将在用户选择特定数字后调用的 switch case 语句中调用它。这部分有 5 个案例陈述(姓名、年龄、课程、一般加权平均数和对所有内容进行排序的选项 - 如果可行,我计划添加更多学生属性)
(我不知道我是否应该将它存储在另一个方法中并在 main 方法中调用它,或者只是将它放在 main 方法中)
public RecordReference Process(RecordReference[] f, RecordReference[] a) {
// for loop?
for (int x = 0; x < f.length; x++) {
switch (choice) {
case 1:
System.out.println("Sorted array of first name: ");
sortFirstNameArray(f[x].getFirstName());
System.out.printf("%-15s%n", Arrays.toString(f));
break;
case 2:
System.out.println("Sorted array of age: ");
// invokes the age method
sortAgeArray(a[x].getAge());
System.out.printf("%-15s%n", Arrays.toString(a));
break;
}
}
}
如果是在另外一个方法中,那么在main方法中调用的时候要包含什么参数呢? 我试过了,但是没用,我不知道该怎么办
System.out.print("Please choose what student attribute you want to
sort :");
choice = keyboard.nextInt();
// calling the process method here, but I receive syntax error
Process(f,a); // Here, I want to pass the sorted values back into the array but I get an error.
如果你能帮助我,那就太好了。提前谢谢你。
我只是一年级学生,我很想学习如何解决这个错误。
很高兴看到您自己尝试了这个问题并更正了您的问题以使其更清楚,因此我愿意提供帮助。
我尽量使问题的解决方案与您的解决方案尽可能接近,以便您能够理解它。可能有更好的方法来解决这个问题,但这不是这里的重点。
首先,让我们创建一个名为 BubbleSorter
的 class 来存放排序方法:
public class BubbleSorter
{
//Explicitly provide an empty constructor for good practice.
public BubbleSorter(){}
//Method that accepts a variable of type RecordReference[], sorts the
//Array based on the firstName variable within each RecordReference
//and returns a sorted RecordReference[].
public RecordReference[] SortByFirstName(RecordReference[] recordReferencesList)
{
for (int i = 0; i < recordReferencesList.length - 1; i++) {
for (int j = i + 1; j < recordReferencesList.length; j++) {
if (recordReferencesList[i].getFirstName().compareToIgnoreCase
(recordReferencesList[j].getFirstName()) > 0) {
RecordReference temp = recordReferencesList[i];
recordReferencesList[i] = recordReferencesList[j];
recordReferencesList[j] = temp;
}
}
}
return recordReferencesList;
}
}
这给了我们一个可以实例化的class,可以在其中添加方法用于排序。我添加了其中一种方法,该方法采用 RecordReference[]
作为参数,并根据每个 RecordReference
中的 firstName
class 变量对 RecordReference[]
进行排序。您将需要添加更多自己的方法来对其他 class 变量进行排序。
现在主要 class:
class Main {
public static void main(String[] args) {
//Get a mock array from the GetMockArray() function.
RecordReference[] refArray = GetMockArray();
//Instantiate an instance of BubbleSorter.
BubbleSorter sorter = new BubbleSorter();
//Invoke the SortByFirstName method contained within the BubbleSorter
//and store the sorted array in a variable of type RecordReference[] named
//sortedResult.
RecordReference[] sortedResult = sorter.SortByFirstName(refArray);
//Print out the results in the sorted array to check if they are in the correct
//order.
//This for loop is not required and is just so that we can see within the
//console what order the objects in the sortedResult are in.
for(int i = 0; i < sortedResult.length; i++)
{
System.out.println(sortedResult[i].getFirstName());
}
}
public static RecordReference[] GetMockArray()
{
//Instantiate a few RecordReferences with a different parameter for
//the firstName in each reference.
RecordReference ref1 = new RecordReference(0, "Ada", "Test", "Test", 22, "First",
"Computer Science", 1.0f);
RecordReference ref2 = new RecordReference(0, "Bob", "Test", "Test", 22, "First",
"Computer Science", 1.0f);
RecordReference ref3 = new RecordReference(0, "David", "Test", "Test", 22,
"First", "Computer Science", 1.0f);
//Create a variable of type RecordReference[] and add the RecordReferences
//Instantiated above in the wrong order alphabetically (Based on their firstName)
//class variables.
RecordReference[] refArray = {
ref2, ref3, ref1
};
return refArray;
}
}
在主要 class 中,我提供了详细的评论来准确解释正在发生的事情。我想指出的一件事是我添加了一个名为 GetMockArray()
的方法。这只是为了提供 RecordReference[]
进行测试,您可能想在您选择的其他地方进行测试。
如果有任何不清楚的地方或您需要更多帮助,请对此答案发表评论,我会尽力帮助您。
谢谢。