簡體   English   中英

如何根據一個數組的值對兩個數組排序

[英]How to sort two arrays according to one array's values

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

如您所見,時間對於冠軍是正確的,但不是按字母順序排序的。

我認為您的問題是您沒有用“冠軍”重新排列“時間”。 從您的示例中可以看出,“時間”似乎只是按照遞增順序排列的年份,而冠軍就是那一年的冠軍隊。 當您將冠軍按字母順序排序時,它們與時間不同步。

要解決此問題,您需要將“時間”與“冠軍”配對,以便如果按其中一個值排序,則另一個將隨之移動。

從這樣的內部類開始:

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

因為我們使新的元組實現可比較,所以我們可以對其進行排序。 元組的compareTo方法按字母順序正確排序。

   Arrays.sort(timechamps);

然后您可以打印出結果

   for (Tuple t : timechamps) {
     System.out.println(t.time+"\t"+t.champ);
   }
 }

您想在這里看看嗎http://www.mkyong.com/java/how-to-sort-a-map-in-java/

這里鍵----->冠軍值->時間

我認為您想縮短冠軍的時間,應該用相應的值保持時間。 希望它對您有所幫助。

最好創建一個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());
    }
}

編輯:編寫一個更具描述性的答案,該答案構造數據集並調用sort方法

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

}

我也去了。 這是一個功能齊全的獨立解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM