【问题标题】:Printing an array from lowest to highest without sorting从最低到最高打印数组而不进行排序
【发布时间】:2016-01-22 06:43:59
【问题描述】:

首先我想说我不是很有经验,如果这个问题得到了回答,我很抱歉。我一直试图找到一个答案,但一直未能。

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

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

}
}

【问题讨论】:

  • 复制数组并对其进行排序。在第二个数组中,您可以跟踪索引,即进行与主数组中相同的交换。
  • 不排序如何排序
  • 当你说“不排序”时,你的意思是:(1)你需要保留原始的排序信息(即[56, 45, 67, 41, 59, 70]),但是复制这个并排序就可以了;或者 (2) 这是某个班级的作业并且禁止排序?

标签: java arrays sorting methods


【解决方案1】:

这是一个开始。

从你的数组中,创建一个排序的地图,比如说

Map&lt;Integer,Integer&gt; mymap = new TreeMap&lt;Integer,Integer&gt;.

您将使用 temp 作为键,使用 day 作为值。例如,根据您的示例数据,

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

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

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

【讨论】:

  • 如果您使用温度作为键,您应该注意,如果两天的温度相同,那么Map 中只会出现最后一个温度
【解决方案2】:

这里有一个小例子来说明如何做到这一点。只对辅助的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

【讨论】:

    【解决方案3】:

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

    将该数组按温度值排序,然后打印出来。

    【讨论】:

      最近更新 更多