-1

我的程序中有三个数组姓氏、名字和结果,需要创建一个排序数组姓氏:克里斯名字:查尔顿:结果:54 姓氏:安妮特:名字:莱尔:结果 67 如果我想按姓氏的字母顺序对其进行排序,我需要移动所有字段,而不仅仅是姓氏。这是我正在处理的冒泡排序代码

    int swap;
    boolean swapflag = true;
    //outer loop
    while (swapflag == true)
    {
        swapflag = false;
        //inner loop
        for (int index=0; index < (nums.length - 1); index++)
        {
            //swap routine
            if (nums[index]> nums[index + 1])
            {   //swap routine
                swap = nums[index];
                nums[index] = nums[index + 1];
                nums[index + 1] = swap;
                swapflag = true;
            }
        }//end inner
    }//end outer

    System.out.println ("\nArray Contents after sorting"
            + "\n*************");

    for (int index=0; index < nums.length; index ++)
    {  
        System.out.println("Array element " 
                + index + ": " + nums[index]);  
    }
}

}

   `     package projStudent;
import java.util.Scanner;
public class UnitResults 
{
    //delcare Scanner as keyb
    static Scanner keyb = new Scanner (System.in);
    //declare fields
    static String studentForename [];
    static String studentSurname [];
    static int [] studentResult;

    static int pointer;

    //constructor
    UnitResults(int sizeofclass)
    {//start of constructor
        studentForename = new String [sizeofclass];
        studentSurname = new String [sizeofclass];
        studentResult = new int [sizeofclass];
        pointer = 0;
    }//end of constructor

    public boolean add(String studentForename[], String studentSurname[],
             int studentResult[])
    {//start of add method
        if (pointer == studentResult.length )
        {//start of if statement
            System.out.println("Sorry Array is full");
            return false;
            studentResult[pointer] = studentResult[];
            pointer ++;
        }//end of if statement


    }//end of add method
    public boolean delete(int element)
    {//start of delete method
        element = element - 1;
        if ((element >= 0) && ( element < pointer))
        {//start of if statement
            for(int index = (element + 1); index < pointer; index++)
            {//start of for statement
                studentResult[index - 1] = studentResult[index];   
            }//end of for statement
            pointer--;
            return true;
        }//end of if statement
        else
        {//start of else statement
            return false;
        }//end of else statement  
    }//end of delete method

       public String find()
    {//start of display
        String strOutput="";
        strOutput = strOutput + "Students";
        if (pointer==0)
        {//start of if statement
            strOutput = strOutput + "There are no records in this Array";
            return strOutput;
        }//end of if statement

    for (int index=0; index < pointer; index++)
    {//start of for method
        strOutput = strOutput + "Student Name" + studentSurname[index] + studentForename + 
                "Student Result" + studentResult +"\n";
    }//end of for method

    return strOutput;
}//display
   public int sort (int UnitResults)

   {//start of sort
       int sort;
       boolean swapflag = true;
       while (swapflag == true)
       {//start of while loop
           swapflag = false;
           for (int index=0; index < (UnitResults - 1); index++)
           {
               if (studentResult[index]> studentResult[index + 1])
            {   //swap routine
                sort = studentResult[index];
                studentResult[index] = studentResult[index + 1];
                studentResult[index + 1] = sort;

                swapflag = true;
            }


           }

       }//end of while loop

   }//end of sort

}`

4

5 回答 5

1

不幸的是,您的帖子令人困惑,因为您没有包含一些内容,例如您正在排序的当前数组是什么。不过,如果我正确理解你的问题......

无论使用哪种语言,您的策略都将涉及更改交换元素的方式。如果您的数组由复合数据组成,那么只需在交换中分配就可以了。如果您的数据是分散的,那么您的交换需要交换每个变量。您始终可以将数组的索引排序到另一个数组中,然后使用该数组间接引用第一个数组以进行排序访问。

于 2012-12-05T13:32:34.880 回答
1

我建议您为此目的使用列表。首先创建一个对象。例如,“Person”包含“Forname”、“Surename”、“Result”的成员。然后用这些对象填充列表,实现接口 Compareable 并使用 Collection.sort() 方法。

class Person implements Comparable<Person>
{
   private String forname;
   private String surname;
   private int rating;

   public Person(String forename, String surname, int rating)
   {
         this.forname = forename;
         this.surname = surname;
         this.rating = rating 
   }

  public int compareTo(Person p) {
       if(p.rating == this.rating) 
          return 0;
       else if(p.rating < this.rating) 
            return -1;

       return 1;  
  }


}

class Test{

    public static void main(String[] args){

        List<Person> personList = new ArrayList<Person>();

         Person p1 = new Person("John","Smith",10);
         Person p2 = new Person("Max","Muster",20);
         Person p3 = new Person("Sarah","Clark",15);

         personList.add(p1);
         personList.add(p2);
         personList.add(p3);

         personList.sort();
    }

}
于 2012-12-05T13:55:55.917 回答
0

Can't understand properly the question: are you looking for a way to manually implement a sorting algorithm (bubble, quick or whatever) or you would like to simply sort them the best you can? Generally speaking you should never implement your own sort 'cause Java provides itself a very efficient lot... or is this an exercise? Probably :)

Best way I can imagine is, provided the 3 arrays in their original form are linked by index, create a surname/index map, load it form surname array, sort the Map.Entry by key and then you will have the array indexes sorted the way you wanted. Check here for more details: how to sort Map values by key in Java

PS The solutions provided by the others are correct and preferrable if you are NOT doing an exercise :) Better deal with a structured object than with 3 separated data.

于 2012-12-05T13:59:18.570 回答
0

Java 编程语言有许多特性可以帮助您解决您遇到的问题,其中第一个是包含适当的数据结构和用于在这些数据结构中操作对象的方法。

首先,我建议使用 java 类来表示一个人的实体……想想看,当你查找一个人的信息时,你不会查阅三本不同的书,或者电脑屏幕,或者你有什么,当所有这些信息都可以组织到一个地方时。例如,对于您上面的人,您可以使用以下内容:

public class Person implements Comparable<Person> {
    public String firstName;
    public String lastName;
    public int result;

    public Person(String fn, String ln, int r) {
        firstName = fn;
        lastName = ln;
        result = r;
    }

    public int compareTo(Person otherPerson) {
       return lastName.compareTo(otherPerson.lastName);
    }

}

这将为您提供一个对象,该对象将存储您的所有个人信息,并且默认情况下可以轻松按姓氏排序(您可以使用比较器更改此行为,我不会在这里介绍。)

现在,您可以拥有一个 Persons 数组,而不是拥有三个不同的名字、姓氏和结果数组。在 java 语言中实际上已经有数组的排序机制,如果您选择,您可以研究和使用它,但是如果您想使用自己的排序,您只需将交换条件替换为如下内容:

if(persons[index].compareTo(persons[index+1]) > 0) {
    ...
}
于 2012-12-05T13:51:02.523 回答
0

我只想问你

为什么要创建班级学生,即

class Student{
     private String studentForename;
     private String studentSurname;
     private int studentResult;
//setters and getters
}

并将它们放入一些集合中,即列表中,您将它们放入 3 个不同的数组中?

您是否意识到,如果您将它们很好地放在列表中,您可以通过使用对它们进行排序Collections.sort()

于 2012-12-05T13:53:35.573 回答