【问题标题】:Sorting with Strings and int in array使用数组中的字符串和 int 进行排序
【发布时间】:2013-09-14 18:14:15
【问题描述】:

我的任务要求我制作一个电视节目节目,我可以在其中输入节目、删除、修改和排序节目。我坚持的是排序部分。该程序提示用户输入节目名称、新剧集首映日期和时间,这些信息存储在数组中。它按键名、日期和时间(按字母顺序和数字顺序)排序。

程序提示用户输入其中一个键,然后程序需要按该键对节目进行排序(按天排序将按字母顺序排序)。

我创建了一个类并使用了一个数组来保存输入的节目。这是课程:

public class showInfo   
{  
String name;   
String day;   
int time;       
}

我输入的节目是这样的:

public static void addShow() throws IOException
    {
    //initialize counter
    int i = 0;
    arr = new showInfo[i];

    showInfo temp = new showInfo();

    //input information
    do
    {
        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        i++;

        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

为了按时间排序,我写了如下方法:

public static void timeSort()
{
    int min;
    for (int i = 0; i < arr.length; i++) 
    {

        // Assume first element is min
        min = i;
        for (int j = i+1; j < arr.length; j++) 
        {
            if (arr[j].time < arr[min].time) 
            {
                min = j;
            }
        }

        if (min != i) 
        {
            int temp = arr[i].time;
            arr[i].time = arr[min].time;
            arr[min].time = temp;
        }
    }
    System.out.println("TV Shows by Time");
    for(int i = 0; i < arr.length; i++)
    {
        System.out.println(arr[i].name + " - " + arr[i].day + " - " + arr[i].time + " hours");
    }
}

我的问题是,当我调用它并在主中输出它时,它只显示“按时间显示的电视节目”文本,而不显示节目。为什么是这样? (我应该显示我的完整代码吗?)

我认为我的问题与计数器有关,但我不确定如何解决这个问题,因为用户应该输入任意数量的节目,并且节目数量已被修改用其他方法删除。

任何帮助都会很棒!提前致谢!

【问题讨论】:

  • 你需要告诉我们你在哪里以及如何填充你的数组。
  • 您是否验证过您的数组长度 > 0...或者您确定在调用该方法时该数组中确实有值?
  • 显示准确的输出总是有帮助的。如果您没有看到“- - - - 小时”...
  • 在timeSort方法开始处添加System.out.println("展示次数="+arr.length);验证您确实有一些节目。
  • 看起来您只是在交换时间,而不是交换数组中的条目。应该是:showInfo temp = arr[i]; arr[i]=arr[分钟]; arr[min]=温度;

标签: java arrays


【解决方案1】:

如果您没有被禁止使用 Java 的 Collections API,请使用 Arrays.sort(T[],Comparator)。然后,您的程序可以维护三个静态 Comparator 对象——一个用于表演,一个用于一天,一个用于时间。例如,

  static Comparator<showTime> daySort = new Comparator<showTime>(){
    public int compare(showTime st1, showTime st2){
      return st1.day.compareTo(st2.day);
    }
    public boolean equals(Object o){
      return this==o; /* Easy way out for this method. */
    }
  };

然后你有一个调用 Arrays.sort 的排序方法(如果它甚至是必要的):

  public static void sort(){
    Arrays.sort(arr, daySort);
  }

请参阅ArraysComparator 了解更多信息。

【讨论】:

  • 问题是,我的排序方式有限。我的老师希望我使用冒泡排序或选择排序。所以我真的不能用这个。 :(
  • 这是有道理的。只要知道你应该这样做 IRL。
【解决方案2】:

我认为排序不起作用,因为在 ShowInfo[] 数组中没有要排序的元素。

您正在addShow 方法中初始化ShowInfo 数组:

//initialize counter
int i = 0;
arr = new showInfo[i];

你正在创建一个新的ShowInfo

ShowInfo temp = new showInfo(); 

但是这个temp 从未分配给任何数组元素。因此,每次调用此方法添加新的show 时,实际上是在创建一个长度为 0 的新 ShowInfo[] 数组和一个从未分配给任何数组元素的 ShowInfo 实例。

您只需初始化数组/计数器一次,因此将数组/计数器initialization 代码与其使用分开(您不应将这些初始化语句保留在任何删除/修改/排序方法中):

private static int i=0;
private static ShowInfo[] arr=new ShowInfo[NO_OF_SHOWINFOS];

有了这些,你的addShow 方法会变成这样:

public static void addShow() throws IOException
    {
    //initialize counter
    //int i = 0;
    //arr = new showInfo[i];

    //showInfo temp = new showInfo();

    //input information
    do
    {
        showInfo temp = new showInfo();

        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        //i++;
        arr[i++]=temp;
        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

}

【讨论】:

  • 在读取输入时,您可以构建一个 List,然后使用 list.toArray 将其转换为数组。这样您就不必在开始输入之前知道节目的数量。
猜你喜欢
  • 2015-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
相关资源
最近更新 更多