2012-12-05 68 views
-1

我有我的proggramme姓,名三個數組和結果,需要創造一種陣列 姓:克里斯用的名字:查爾頓:結果:54 姓:阿內特:用的名字:萊爾:導致67 如果我想對它進行排序通過姓氏的字母順序,我需要所有的字段移動,而不僅僅是姓氏。這是冒泡排序的代碼我工作過如何在java中對三個數組進行排序?

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 

}`

+2

** Java **和** JavaScript **是兩種完全不同的語言。 – Pointy

+2

爲什麼你使用3個數組,而不是一個?你不能把屬於一個人的三個值合併到一個Person類或其他東西中,然後把它放到一個數組中嗎? –

回答

1

不幸的是,您的文章是混亂的,你不包括一些事情,比如正是是你正在排序的當前數組。不過,如果我正確理解你的問題......

無論語言如何,你的策略都會涉及到你如何交換元素的變化。如果你的數組由複合數據組成,那麼只需在交換中分配就可以了。如果你的數據是分散的,那麼你的交換需要交換每個變量。您始終可以將數組的索引排序到另一個數組中,然後使用該數組間接引用第一個數組,以進行排序訪問。

+0

我打算建議一組索引。保持其他三個數組的順序相同,然後按照需要對索引數組進行排序。 –

+0

這是我現在的代碼,我只是用我的一個數組studentResult來嘗試它。我在我的評論中添加了代碼 –

+0

我的所有當前代碼都在帖子中 –

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

} 

這會給你一個對象,將存儲所有的個人信息,並且默認情況下會按姓氏容易排序(你可以用一個比較器來改變這種行爲,我不會在這裏討論)

現在不是有三個不同的名字,姓氏和結果數組,你可以有一個單一的人員數組。實際上,在java語言中已經有了用於數組的排序機制,如果您選擇,您可以研究和使用這些排序機制,但是如果您想使用自己的排序機制,則只需要用以下類似方式替換條件:

if(persons[index].compareTo(persons[index+1]) > 0) { 
    ... 
} 
0

我只想問你

爲什麼,而不是創建類的學生即

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

,並把它們在一些集合即列表 你把他們放到3個不同的陣列?

你知道嗎,如果你在列表中有很好的名字,你可以使用Collections.sort()來排序嗎?

1

我建議你爲此使用一個列表。 首先創建一個對象。例如包含「Forname」,「Surename」,「Result」成員的「Person」。然後用這些對象填充列表,實現Interface 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(); 
    } 

} 
0

不能正確理解這個問題:你在尋找一種方式來手動實現排序算法(氣泡,快速或其他),或者你想簡單地對它們進行排序最好的,你可以嗎?一般來說,你不應該實現你自己的排序'因爲Java提供了一個非常有效的批次......或者這是一個練習嗎?可能:)

我可以想象的最佳方式是,如果3個數組以原始形式鏈接索引,創建一個姓氏/索引映射,加載它形成surname數組,按鍵排序Map.Entry然後你將按照您想要的方式排序數組索引。在這裏查看更多詳細信息:how to sort Map values by key in Java

PS其他人提供的解決方案是正確的,如果您沒有進行鍛鍊,則更喜歡。:)更好地處理結構化對象而不是3分離數據。