簡體   English   中英

從低到高打印陣列而不進行排序

[英]Printing an array from lowest to highest without sorting

首先,我想說我不是非常有經驗,如果對此得到答復,對不起。 我已經試圖尋找答案已有一段時間了,但一直未能成功。

我正在一個項目中,用戶將數字輸入到數組中。 這些數字代表不同日期的溫度。 天顯然是數組中的位置。 我需要找到一種在不對數組進行排序的情況下從最低到最高打印溫度的方法。

因此,如果用戶輸入[56、45、67、41、59、70],則表示在位置0(第1天)為56度,在位置2(第3天)為67度。 我需要保持數組的位置不變,這樣打印出來的時間就與時間保持一致。

編輯:到目前為止,我已經附加了我在項目中擁有的代碼。 HighestOrdered方法是我不知道該做什么或從哪里開始的方法。 對於我上面所說的HighestOrdered方法,我需要讓它打印出日期與日期(數組中的位置)的時間,但我不確定該怎么做。

這是我到目前為止的代碼:

public class Weather {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int [] high = new int[30];
    int [] low = new int[30];

    Init (high);
    Init(low);


    LoadData(high,low);
    Report(high, low);

    FindAvg(high,low);
    Lowest(high, low);
    Highest(high,low);
}
public static void Init(int A[])
{
    for(int i = 0; i < A.length; i++)
    {
        A[i] = 510;
    }
}

public static void Report(int[] H, int[] L)
{
    System.out.println("Day    High    Low");

    for(int i = 0; i < H.length; i++)
    {
        System.out.println(i + "      " + H[i] + "      " + L[i]);
    }
}
public static void LoadData(int[] H, int[] L)
{

    int day = 0;
    while(day < 30)
    {
        try {
            int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
            H[day] = high;
        } catch (NumberFormatException e) {
        }
        try {
            int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));
            L[day] = low;
        } catch (NumberFormatException e) {
        }
        day++;        

    }       
}
public static void FindAvg(int[] H, int[] L){

    int sumHigh = 0;
    int avgHigh;

    int sumLow = 0;
    int avgLow;

    for(int i : H)
        sumHigh += i;
    avgHigh = sumHigh/H.length;

    for(int i : L)
        sumLow += i;
    avgLow = sumLow/L.length;

    System.out.println("The average for the high is: " + avgHigh);
    System.out.println("The average for the low is: " + avgLow);
}
public static void Highest(int[] H, int[] L)
{
    int highestHigh = -1000;
    int dayHigh = 0;

    int highestLow = -1000;
    int dayLow = 0;

    for(int i = 0; i < H.length; i++)
    {
        if(H[i] > highestHigh && H[i] != 510)
        {
            highestHigh = H[i];
            dayHigh = i;
        }
    }
    System.out.println("\n" + "The highest high is: " + highestHigh + " degrees." + "\n" +
            "This temperature was recorded on day: " + dayHigh);    

    for(int i = 0; i < L.length; i++)
    {
        if(L[i] > highestLow && L[i] != 510)
        {
            highestLow = L[i];
            dayLow = i;
        }
    }
    System.out.println("\n" + "The highest low is: " + highestLow + " degrees." + "\n" +
            "This temperature was recorded on day: " + dayLow);
}

public static void Lowest(int[] H, int[] L)
{

    int lowestHigh = 1000;
    int dayHigh = 0;

    int lowestLow = 1000;
    int dayLow = 0;

    for(int i = 0; i < H.length; i++)
    {
        if(H[i] < lowestHigh)
        {
            lowestHigh = H[i];
            dayHigh = i;    
        }   
    }
    System.out.println("\n" + "The lowest high is: " + lowestHigh + " degrees." + "\n" +
            "This temperature was recorded on day: " + dayHigh);

    for(int i = 0; i < L.length; i++)
    {
        if(L[i] < lowestLow)
        {
            lowestLow = L[i];
            dayLow = i;
        }
    }
    System.out.println("\n" + "The lowest low is: " + lowestLow + " degrees." + "\n" +
            "This temperature was recorded on day: " + dayLow); 
}

public void HighestOrdered(int[] H)
{

}
}

這是一個開始。

在您的數組中,創建一個排序的地圖,例如

Map<Integer,Integer> mymap = new TreeMap<Integer,Integer>

您將使用temp作為鍵,使用day作為值。 例如,根據您的示例數據,

myMap.put(56,1); myMap.put(45,2);

(注意-在真實代碼中,您將遍歷數組以放置值。)

然后,您可以遍歷myMap中的鍵和值(或條目)。

您可以創建一個對象數組,而不是當前數組,每個對象都包含兩個元素:日期和相應的溫度。

按溫度值對該數組排序,然后打印。

這是一個小例子,說明如何做到這一點。 僅對輔助index數組進行排序,原始temp數組未更改。

public static void main(String[] args) {
    final int [] temp = {56, 45, 67, 41, 59, 70};

    Integer [] index = new Integer[temp.length];
    for (int i = 0; i < index.length; i++) {
        index[i] = i;
    }

    Arrays.sort(index, new Comparator<Integer>() {
        @Override
        public int compare(Integer a, Integer b) {
            return temp[a] - temp[b];
        }
    });

    for (Integer i : index) {
        System.out.printf("temp %d on day %d%n", temp[i], i);
    }

}

這給出了輸出:

temp 41 on day 3
temp 45 on day 1
temp 56 on day 0
temp 59 on day 4
temp 67 on day 2
temp 70 on day 5

暫無
暫無

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

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