从最低到最高打印数组而不进行排序

时间:2016-01-22 06:43:59

标签: java arrays sorting methods

我想首先说我不是很有经验,如果有人回答,我很抱歉。我一直试图找到一个答案,但一直没能。

我正在开发一个用户将数字输入数组的项目。这些数字代表不同天数的温度。这些日子显然是阵中的位置。我需要找到一种方法来打印从最小到最大的温度,而无需对阵列进行排序。

因此,如果用户输入[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)
{

}
}

3 个答案:

答案 0 :(得分:1)

这是一个开始。

从数组中创建一个有序地图,比如说

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

您将使用temp作为键和值的日期。例如,来自您的示例数据,

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

(注意 - 在实际代码中,您将迭代数组以放置值。)

然后你可以遍历myMap中的键和值(或条目)。

答案 1 :(得分:0)

您可以创建一个对象数组,而不是当前数组,每个对象都有两个元素:日期和相应的温度。

按温度值对此数组进行排序,然后打印出来。

答案 2 :(得分:0)

这是一个小例子,展示了如何做到这一点。只对辅助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