2009-06-20 141 views
3

我想将2个数组传递给Java中的函数,并在调用函数中对它们进行排序。我如何使用一个函数来实现这个功能?使用Java对数组进行排序

我可以让函数返回一个包含2个数组的对象,但是有没有一个非面向对象的解决方案?

编辑:我这种特殊的情况下,我不能使用Java中的内置Array.sort函数。可以说2阵列是高度和重量。它们的长度相同,并且相同的索引对应于两个阵列上同一人的身高和体重。我想按升序对高度数组进行排序,同时对高度数组对应的权重数组进行排序。所以使用sort函数会搞乱2个数组之间的关系。

回答

4

将数组传递给函数时,它不会被复制。只是将其引用复制并传递给将指向相同位置的函数。你只需要就地排列数组。

编辑:为了解决实际的排序问题,你可以使用任何排序算法来排序height数组。唯一不同的是,当你在height排序过程中交换两个元素时,还应该交换weight数组中的相应元素。

5
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说的那样,你可以手动实现一个并行数组排序算法,但实际上并不理想。

0

所以你想要函数A来调用函数B.然后B排序数组和A取回两个排序的数组?

由于参数是Java中的引用,如果您在B中修改对象,则A将看到修改后的版本。

在C#中甚至可以用out关键字来明确地说,它告诉大家该函数将修改out参数。

+0

这是错误的。 out意味着调用者的object []变量会指向一个新的对象[]。这不是这里发生的事情。 – 2009-06-20 19:10:13

1

虽然使用两个单独的数组并保持排序同步是可能的,但使用这种类型的解决方案可能会导致以后很难找到的错误。例如,如果阵列之间的同步不能正常工作,那么错误的权重可能会与高度匹配。

避免此类问题的一种方法是将高度/重量封装在一个类中,以便它们始终保持同步。在图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");  
    } 

} 

0

可以返回数组的数组或含有两个阵列的对象。但是,这听起来像两个数组中的值应该相关,所以您应该确实有一个包含这两个值的对象数组。

顺便说一句:我不会使用Float永远浮动我会避免以及(因为它只准确到6个地方)我会建议使用int,long或double。