0
public static void getSort(short[] time, String[] champs){
    System.out.println("Time     Champs\n");
    for(int a= 0; a < time.length; a++){
        char Fletter=champs[a].charAt(0);
        if('B' == Fletter){
            Arrays.sort(champs);
            System.out.println(time[a] + "     " + champs[a]);
        }
    }
    for(int a= 0; a < time.length; a++){
        char Fletter=champs[a].charAt(0);
        if('C' == Fletter){
            Arrays.sort(champs);
            System.out.println(time[a] + "     " + champs[a]);
        }
    }
}

大家好,我需要有关此功能的一些建议和帮助。我想要做的是输出并显示数组时间和冠军里面的内容。

我想要的输出是:

Time----Champs

2001 Banana   

2004 Banana

2000 Boat

2003 Boat

2011 Carrot

2013 Carrot

2002 Cucumber

正确显示时间和冠军的位置按字母顺序显示但是当我使用Arrays.sort(champs);

我的输出是:

Time----Champs

2004  Banana

2005  Banana

2006  Boat

2007  Boat

2008  Carrot

2009  Carrot

2010  Cucumber

冠军的输出显示正确,但年份按 1 递减列出。

没有Arrays.sort(champs)我的输出是:

Time----Champs

2000 Boat

2001 Banana  

2003 Boat

2004 Banana

2002 Cucumber

2011 Carrot

2013 Carrot

如您所见,冠军的时间是正确的,但未按字母顺序排序。

4

4 回答 4

2

我认为您的问题是您没有用“冠军”重新安排“时间”。从您的示例来看,似乎“时间”只是按递增顺序排列的年份,而冠军就是那一年的冠军队伍。当您按字母顺序对 Champs 进行排序时,它们与时间不同步。

为了解决这个问题,您需要将 Time 与 Champs 配对,这样如果您按其中一个值排序,另一个值会随之移动。

从这样的内部类开始:

public static class Tuple implements Comparable<Tuple>{
    public short time;
    public String champ;

    public Tuple(short time, String champ) {
        this.time = time;
        this.champ = champ;
    }

    public int compareTo(Tuple other) {
        return this.champ.compareTo(other.champ);
    }
}

然后,在哪里改变你当前的方法来制作一个元组数组:

public static void getSort(short[] time, String[] champs){
   // assuming time.length & champ.length are the same
   Tuple[] timechamps = new Tuple[time.length];
   for (int a = 0; a < time.length; a++) {
     timechamps[a] = new Tuple(time[a], champs[a]);
   }

因为我们已经使新的元组实现了 Comparable,所以我们可以简单地对其进行排序。元组的 compareTo 方法按字母顺序正确排序。

   Arrays.sort(timechamps);

然后你可以打印出结果

   for (Tuple t : timechamps) {
     System.out.println(t.time+"\t"+t.champ);
   }
 }
于 2013-04-18T05:59:40.813 回答
1

你想看看这里 http://www.mkyong.com/java/how-to-sort-a-map-in-java/

这里键 -----> 冠军价值观 --> 时间

我认为你想缩短你的冠军,时间应该保持相应的价值。希望它对你有帮助。

于 2013-04-18T05:58:06.200 回答
0

我也去了。这是一个功能齐全、独立的解决方案。

import java.util.Arrays;

public class TimeAndChampSorter {

    public static void main(String[] args) {
        // prepare input
        short[] time = { 2000, 2001, 2003, 2004, 2002, 2011, 2013 };
        String[] champs = { "Boat", "Banana", "Boat", "Banana", "Cucumber",
                "Carrot", "Carrot" };
        printSorted(time, champs);
    }

    public static void printSorted(short[] time, String[] champs) {
        // check arguments are of equal length
        if (time.length != champs.length) {
            throw new IllegalArgumentException(
                    "arrays time and champs must have equal length");
        }
        Tuple[] tuples = createTuples(time, champs);
        Arrays.sort(tuples);
        printTuples(tuples);
    }

    private static Tuple[] createTuples(short[] time, String[] champs) {
        // create empty array of Tuples with correct length
        Tuple[] tuples = new Tuple[champs.length];
        // fill the tuples array
        for (int i = 0; i < champs.length; i++) {
            tuples[i] = new Tuple(time[i], champs[i]);
        }
        return tuples;
    }

    private static void printTuples(Tuple[] tuples) {
        System.out.println("Time     Champs\n");
        for (Tuple tuple : tuples) {
            System.out.println(tuple);
        }
    }

    // static class to avoid having to create an instance of TimeAndChampSorter
    static class Tuple implements Comparable<Tuple> {
        short time;
        String champ;

        Tuple(short time, String champ) {
            // make sure champ is not null to avoid having to test for nulls in
            // compareTo
            if (champ == null) {
                throw new IllegalArgumentException("champ can not be null");
            }
            this.time = time;
            this.champ = champ;
        }

        // method of Comparable interface determines the ordering
        @Override
        public int compareTo(Tuple other) {
            return this.champ.compareTo(other.champ);
        }

        @Override
        public String toString() {
            return time + "     " + champ;
        }
    }
}
于 2013-04-18T08:05:10.573 回答
0

最好创建一个 Class 并实现 Comparable 方法

public class TimeChamp implements Comparable<TimeChamp>{

private short time;
private String champs;

public short getTime() {
    return time;
}

public void setTime(short time) {
    this.time = time;
}

public String getChamps() {
    return champs;
}

public void setChamps(String champs) {
    this.champs = champs;
}

@Override
public int compareTo(TimeChamp o) {
    if(getChamps().compareTo(o.getChamps())==0)
    {
        if(getTime()<o.getTime())
            return -1;
        else
            return 1;
    }
    else
        return getChamps().compareTo(o.getChamps());
}
}

然后您可以创建将在您的类中排序的函数,如下所示

public void getSort(TimeChamp[] timeChamp)
{
    Arrays.sort(timeChamp);
    for(int i=0;i<timeChamp.length;i++)
    {   
        System.out.print(timeChamp[i].getTime());
        System.out.print("            ");
        System.out.println(timeChamp[i].getChamps());
    }
}

编辑:编写一个更具描述性的答案,构建数据集并调用排序方法

import java.util.Arrays;


public class MainExample {

/**
 * @param args
 */
public static void main(String[] args) {
            //Construct data sets
    TimeChamp [] timeChampArray = new TimeChamp[3];
    TimeChamp timeChamp = new TimeChamp();
    timeChamp.setChamps("Boat");
    timeChamp.setTime((short)2001);
    timeChampArray[0]=timeChamp;
    timeChamp= new TimeChamp();
    timeChamp.setChamps("Banana");
    timeChamp.setTime((short)2000);
    timeChampArray[1]=timeChamp;
    timeChamp= new TimeChamp();
    timeChamp.setChamps("Banana");
    timeChamp.setTime((short)2001);
    timeChampArray[2]=timeChamp;
            //Call the sort method
    new MainExample().getSort(timeChampArray);
}
//the sorting method
public void getSort(TimeChamp[] timeChamp)
{
    Arrays.sort(timeChamp);
    for(int i=0;i<timeChamp.length;i++)
    {   
        System.out.print(timeChamp[i].getTime());
        System.out.print("            ");
        System.out.println(timeChamp[i].getChamps());
    }
}

}

于 2013-04-18T06:12:13.593 回答