我想將2個數組傳遞給Java中的函數,並在調用函數中對它們進行排序。我如何使用一個函數來實現這個功能?使用Java對數組進行排序
我可以讓函數返回一個包含2個數組的對象,但是有沒有一個非面向對象的解決方案?
編輯:我這種特殊的情況下,我不能使用Java中的內置Array.sort函數。可以說2陣列是高度和重量。它們的長度相同,並且相同的索引對應於兩個陣列上同一人的身高和體重。我想按升序對高度數組進行排序,同時對高度數組對應的權重數組進行排序。所以使用sort函數會搞亂2個數組之間的關係。
我想將2個數組傳遞給Java中的函數,並在調用函數中對它們進行排序。我如何使用一個函數來實現這個功能?使用Java對數組進行排序
我可以讓函數返回一個包含2個數組的對象,但是有沒有一個非面向對象的解決方案?
編輯:我這種特殊的情況下,我不能使用Java中的內置Array.sort函數。可以說2陣列是高度和重量。它們的長度相同,並且相同的索引對應於兩個陣列上同一人的身高和體重。我想按升序對高度數組進行排序,同時對高度數組對應的權重數組進行排序。所以使用sort函數會搞亂2個數組之間的關係。
將數組傳遞給函數時,它不會被複制。只是將其引用複製並傳遞給將指向相同位置的函數。你只需要就地排列數組。
編輯:爲了解決實際的排序問題,你可以使用任何排序算法來排序height
數組。唯一不同的是,當你在height
排序過程中交換兩個元素時,還應該交換weight
數組中的相應元素。
public void sort2(Object o1[], Object o2[])
{
Arrays.sort(o1);
Arrays.sort(o2);
}
稍微複雜:
public <T> void sort2(T o1[], T o2[], Comparator<? super T> c)
{
Arrays.sort(o1, c);
Arrays.sort(o2, c);
}
編輯:一般來說,當你使用並行陣列,您不使用對象正確意思。按照你的例子,你應該有一個Comparable Person類,它有高度和重量屬性。當然,就像Mehrdad說的那樣,你可以手動實現一個並行數組排序算法,但實際上並不理想。
所以你想要函數A來調用函數B.然後B排序數組和A取回兩個排序的數組?
由於參數是Java中的引用,如果您在B中修改對象,則A將看到修改後的版本。
在C#中甚至可以用out關鍵字來明確地說,它告訴大家該函數將修改out參數。
雖然使用兩個單獨的數組並保持排序同步是可能的,但使用這種類型的解決方案可能會導致以後很難找到的錯誤。例如,如果陣列之間的同步不能正常工作,那麼錯誤的權重可能會與高度匹配。
避免此類問題的一種方法是將高度/重量封裝在一個類中,以便它們始終保持同步。在圖1中,有一個名爲Person
的類,它具有高度,重量和名稱作爲屬性。如果你總是會被高度升序進行排序,那麼你就可以實現compareTo()
方法,如圖1所示。
圖2顯示了JUnit測試案例來說明如何排序的Person
的List。測試用例還演示瞭如何按重量排序。在這兩種情況下,重量和高度之間永遠不會有同步問題,因爲排序位於封裝它們的對象上。
圖1 - Person
類
public class Person implements Comparable {
private Float height;
private Float weight;
private String name;
public Person(){}
public Person(Float height, Float weight, String name) {
this.height = height;
this.weight = weight;
this.name = name;
}
public Float getHeight() {
return height;
}
public void setHeight(Float height) {
this.height = height;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int compareTo(Person other) {
//sort by height ascending
return this.height.compareTo(other.getHeight());
}
}
圖2 - JUnit測試類
import junit.framework.TestCase;
import java.util.*;
public class PersonTest extends TestCase {
private List personList = new ArrayList();
public PersonTest(String name) {
super(name);
}
public void testCompareTo() {
personList.add(new Person(72F,125F,"Bob"));// expect 3rd when sorted by height asc
personList.add(new Person(69.9F,195F,"Jack"));// expect 2nd when sorted by height asc
personList.add(new Person(80.05F,225.2F,"Joe"));// expect 4th when sorted by height asc
personList.add(new Person(57.02F,89.9F,"Sally"));// expect 1st when sorted by height asc
Collections.sort(personList);
assertEquals("Sally should be first (sorted by height asc)",personList.get(0).getName(),"Sally");
assertEquals("Jack should be second (sorted by height asc)",personList.get(1).getName(),"Jack");
assertEquals("Bob should be third (sorted by height asc)",personList.get(2).getName(),"Bob");
assertEquals("Joe should be fourth (sorted by height asc)",personList.get(3).getName(),"Joe");
Collections.sort(personList,new Comparator() {
public int compare(Person p1, Person p2) {
//sort by weight ascending
return p1.getWeight().compareTo(p2.getWeight());
}
});
assertEquals("Sally should be first (sorted by weight asc)",personList.get(0).getName(),"Sally");
assertEquals("Bob should be second (sorted by weight asc)",personList.get(1).getName(),"Bob");
assertEquals("Jack should be third (sorted by weight asc)",personList.get(2).getName(),"Jack");
assertEquals("Joe should be fourth (sorted by weight asc)",personList.get(3).getName(),"Joe");
}
}
可以返回數組的數組或含有兩個陣列的對象。但是,這聽起來像兩個數組中的值應該相關,所以您應該確實有一個包含這兩個值的對象數組。
順便說一句:我不會使用Float永遠浮動我會避免以及(因爲它只准確到6個地方)我會建議使用int,long或double。
這是錯誤的。 out意味着調用者的object []變量會指向一個新的對象[]。這不是這裏發生的事情。 – 2009-06-20 19:10:13