Java中的数组排序
我想在Java中将2个数组传递给一个函数,并在调用函数中对它们进行排序。如何使用函数来实现这一点 我可以让函数返回一个包含2个数组的对象,但是有没有非面向对象的解决方案 编辑:在这种情况下,我无法在Java中使用内置的Array.sort函数。假设这两个数组是高度和重量。它们的长度相同,在两个数组中,相同的指数对应于同一个人的身高和体重。我想按升序对高度数组进行排序,同时对与高度数组对应的权重数组进行排序。因此,使用sort函数会弄乱两个数组之间的关系Java中的数组排序,java,arrays,sorting,Java,Arrays,Sorting,我想在Java中将2个数组传递给一个函数,并在调用函数中对它们进行排序。如何使用函数来实现这一点 我可以让函数返回一个包含2个数组的对象,但是有没有非面向对象的解决方案 编辑:在这种情况下,我无法在Java中使用内置的Array.sort函数。假设这两个数组是高度和重量。它们的长度相同,在两个数组中,相同的指数对应于同一个人的身高和体重。我想按升序对高度数组进行排序,同时对与高度数组对应的权重数组进行排序。因此,使用sort函数会弄乱两个数组之间的关系 public void sort2(Obj
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);
}
public void sort2(to1[],t2[],Comparator当您将数组传递给函数时,它不会被复制。只会将其引用复制并传递给指向相同位置的函数。您只需将数组排序到位即可
编辑:为了解决实际的排序问题,您可以使用任何排序算法对height
数组进行排序。唯一的区别是,当您在height
排序过程中交换两个元素时,您还应该交换weight
数组中相应的元素。因此您希望函数A调用函数B。然后B对数组进行排序,A返回两个排序的数组
由于参数在Java中是引用,如果在B中修改对象,A将看到修改后的版本
在C#中,甚至可以用out关键字明确表示,它告诉每个人函数将修改out参数。当使用两个单独的数组并保持排序同步时,使用这种类型的解决方案可能会导致以后很难找到的错误。例如,如果数组之间的同步不起作用这样,错误的权重可能与高度匹配
避免此类问题的一种方法是将height/weight封装在类中,以便它们始终保持同步。在图1中,有一个名为Person
的类将height、weight和name作为属性。如果您总是要按高度升序排序,则可以实现compareTo()
方法,如图1所示
图2显示了一个junit测试用例,用于演示如何对Person
s列表进行排序。该测试用例还演示了如何按权重进行排序。在这两种情况下,权重和高度之间从不存在同步问题,因为排序在封装它们的对象上
图1-人员
类别
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,我也会避免使用Float(因为它只精确到6个位置),我建议使用int、long或double。这是错误的。out意味着调用方的object[]变量将指向一个新的object[]。这不是这里发生的事情。