1

我的任务要求我制作一个电视节目节目,我可以在其中输入节目、删除、修改和排序节目。我坚持的是排序部分。在节目中,它会询问名称、新剧集首播日期和时间。这些是我需要对其进行排序的键。

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

我做了一个类并使用了一个数组。这是课程:

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

以及代码中按时间排序的方法:

public static void intSort()
{
    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");
    }
}

当我调用它并在主中输出它时,它只显示“按时间显示的电视节目”而不是列表。为什么是这样?

此外,我需要制作一种方法,我将能够使用它来对日期和名称(两个字符串)进行排序。在方法中不使用那些特定的数组(arr[i].name、arr[i].day),我怎么能做到这一点?

任何帮助将不胜感激!提前致谢!

4

3 回答 3

3

在这部分代码中

if (min != i) {
    int temp = arr[i].time;
    arr[i].time = arr[min].time;
    arr[min].time = temp;
}

您只是在更改应该移动整个对象的时间。要修复它,代码的行为必须如下所示:

if (min != i) {
    //saving the object reference from arr[i] in a temp variable
    showInfo temp = arr[i];
    //swapping the elements
    arr[i] = arr[min];
    arr[min] = temp;
}

I̶t̶ ̶w̶o̶u̶l̶d̶ ̶b̶e̶ ̶b̶e̶t̶t̶e̶r̶ ̶t̶o̶ ̶u̶s̶e̶ ̶ Arrays#sort ̶w̶h̶e̶r̶e̶ ̶y̶o̶u̶ ̶p̶r̶o̶v̶i̶d̶e̶ ̶a̶ ̶c̶u̶s̶t̶o̶m̶ ̶ ̶C̶o̶m̶p̶a̶r̶a̶t̶o̶r̶̶ ̶o̶f̶ ̶t̶h̶e̶ ̶c̶l̶a̶s̶s̶ ̶b̶e̶i̶n̶g̶ ̶s̶o̶r̶t̶e̶d̶ ̶(̶i̶f̶ ̶y̶o̶u̶ ̶a̶r̶e̶ ̶a̶l̶l̶o̶w̶e̶d̶ ̶t̶o̶ ̶u̶s̶e̶ ̶t̶h̶i̶s̶ ̶a̶p̶p̶r̶o̶a̶c̶h̶)̶.̶ ̶S̶h̶o̶r̶t̶ ̶e̶x̶a̶m̶p̶l̶e̶:̶

showInfo[] showInfoArray = ...
//your array declared and filled with data
//sorting the array
Arrays.sort(showInfoArray, new Comparator<showInfo>() {
    @Override
    public int compare(showInfo showInfo1, showInfo showInfo2) {
        //write the comparison logic
        //basic implementation
        if (showInfo1.getTime() == showInfo2.getTime()) {
             return showInfo1.getName().compareTo(showInfo2.getName());
        }
        return Integer.compare(showInfo1.getTime(), showInfo2.getTime());
    }
});
//showInfoArray will be sorted...

由于您必须使用定制的排序算法并支持不同的数据排序方式,因此您只需更改比较数据的方式。这意味着,在您当前的代码中,更改这部分

if (arr[j].time < arr[min].time) {
    min = j;
}

对于更通用的东西,比如

if (compare(arr[j], arr[min]) < 0) {
    min = j;
}

您只需要compare通过您需要的方法来更改方法的实现。尽管如此,创建和维护一个可以支持不同方式来比较数据的方法还是太复杂了。所以最好的选择似乎是 a Comparator<showInfo>,让你的代码看起来像这样:

if (showInfoComparator.compare(arr[j], arr[min]) < 0) {
    min = j;
}

其中showInfoComparator保存了比较元素的逻辑。现在你intSort会变成更通用的东西:

public static void genericSort(Comparator<showInfo> showInfoComparator) {
    //your current implementation with few modifications
    //...
    //using the comparator to find the minimum element
    if (showInfoComparator.compare(arr[j], arr[min]) < 0) {
        min = j;
    }
    //...
    //swapping the elements directly in the array instead of swapping part of the data
    if (min != i) {
        int temp = arr[i].time;
        arr[i].time = arr[min].time;
        arr[min].time = temp;
    }
    //...
}

现在,您只需编写一组Comparator<showInfo>支持您的自定义标准的实现。例如,这是一个使用该字段比较showInfo实例的例子:time

public class ShowInfoTimeComparator implements Comparator<showInfo> {
    @Override
    public int compare(showInfo showInfo1, showInfo showInfo2) {
        //write the comparison logic
        return Integer.compare(showInfo1.getTime(), showInfo2.getTime());
    }
}

另一个使用该name字段的比较器:

public class ShowInfoNameComparator implements Comparator<showInfo> {
    @Override
    public int compare(showInfo showInfo1, showInfo showInfo2) {
        //write the comparison logic
        return showInfo1.getName().compareTo(showInfo2.getName());
    }
}

现在在您的代码中,您可以这样称呼它1

if (*compare by time*) {
    genericSort(showInfoArray, new ShowInfoTimeComparator());
}
if (*compare by name*) {
    genericSort(showInfoArray, new ShowInfoNameComparator());
}
if (*another custom rule*) {
    genericSort(showInfoArray, new ShowInfoAnotherCustomRuleComparator());
}

现在您可以在哪里实现自定义规则,例如showInfo使用两个或多个字段比较对象。name以您的和day字段为例(如问题中所述):

public class ShowInfoNameAndDayComparator implements Comparator<showInfo> {
    @Override
    public int compare(showInfo showInfo1, showInfo showInfo2) {
        //write the comparison logic
        int nameComparisonResult = showInfo1.getName().compareTo(showInfo2.getName());
        if (nameComparisonResult == 0) {
            return showInfo1.getDay().compareTo(showInfo2.getDay());
        }
        return nameComparisonResult;
    }
}

1:还有其他方法可以解决这个问题,而不是使用大量if语句,但看起来这超出了问题范围。如果没有,请编辑问题并添加它以显示解决此问题的另一种方法。


当前代码的其他提示:

  • 使用 CamelCase 声明类的名称,其中类名的第一个字母是大写,因此您的showInfo类必须重命名为ShowInfo.
  • 要访问类的字段,请使用适当的 getter 和 setter,而不是将字段标记为public或离开 withdefault范围。这意味着,你的ShowInfo班级应该变成:

    public class ShowInfo {
        private String name;
        private String day; 
        private int time;
        public String getName() {
            return this.name;
        }
        public void setName(String name) {
            this.name = name;
        }
        //similar for other fields in the class
    }
    
于 2013-09-01T09:35:58.263 回答
1

为什么不使用Collection来完成这种工作。此外,在您添加的示例中,您只是更改给定对象的一个​​属性,同时在给定列表中进行排序,尽管您没有更改整个对象的位置。

创建一个List将包含所有 的引用Shows,现在ShowList. 一旦算法感觉需要完成交换,只需从 中选择引用List,将其保存在temp变量中,在此位置用新的替换它reference,并将重复设置为存储在temp变量中的那个。你完成了,List排序:-)

这是一个相同的小例子,寻求帮助:

import java.io.*;
import java.util.*;

public class Sorter {

    private BufferedReader input;
    private List<ShowInfo> showList;

    public Sorter() {
        showList = new ArrayList<ShowInfo>();
        input = new BufferedReader(
            new InputStreamReader((System.in)));
    }

    private void createList() throws IOException {
        for (int i = 0; i < 5; i++) {           
            System.out.format("Enter Show Name :");
            String name = input.readLine();
            System.out.format("Enter Time of the Show : ");
            int time = Integer.parseInt(input.readLine());
            ShowInfo show = new ShowInfo(name, time);
            showList.add(show);
        }
    }

    private void performTask() {
        try {
            createList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        sortByTime(showList);
    }

    private void sortByTime(List<ShowInfo> showList) {
        int min;
        for (int i = 0; i < showList.size(); i++) {

            // Assume first element is min
            min = i;
            for (int j = i+1; j < showList.size(); j++) {
                if (showList.get(j).getTime() < 
                            showList.get(min).getTime()) {
                    min = j;
                }
            }

            if (min != i) {
                ShowInfo temp = showList.get(i);
                showList.set(i, showList.get(min));
                showList.set(min, temp);
            }
        }
        System.out.println("TV Shows by Time");
        for(int i = 0; i < showList.size(); i++) {
            System.out.println(showList.get(i).getName() +
                " - " + showList.get(i).getTime());
        }
    }

    public static void main(String[] args) {
        new Sorter().performTask();
    }
}

class ShowInfo {
    private String name;
    int time;

    public ShowInfo(String n, int t) {
        name = n;
        time = t;
    }

    public String getName() {
        return name;
    }

    public int getTime() {
        return time;
    }
}

编辑 2:

对于排序By Name,您可以使用此功能:

private void sortByName(List<ShowInfo> showList) {
    int min;
    for (int i = 0; i < showList.size(); i++) {         
        // Assume first element is min
        min = i;
        for (int j = i+1; j < showList.size(); j++) {               
            int value = (showList.get(j).getName()).compareToIgnoreCase(
                                            showList.get(min).getName());
            if (value < 0)
                min = j;
        }

        if (min != i) {
            ShowInfo temp = showList.get(i);
            showList.set(i, showList.get(min));
            showList.set(min, temp);
        }
    }
    System.out.println("TV Shows by Time");
    for(int i = 0; i < showList.size(); i++) {
        System.out.println(showList.get(i).getName() +
                      " - " + showList.get(i).getTime());
    }
}

编辑 3:

向现有类添加Comparable<?>了接口,以根据指定的输入执行排序。尽管可以通过使用来改进逻辑,Enumeration但将其留给 OP 尝试使用 :-)

import java.io.*;
import java.util.*;

public class Sorter {

    private BufferedReader input;
    private List<ShowInfo> showList;
    private int command;

    public Sorter() {
        showList = new ArrayList<ShowInfo>();
        input = new BufferedReader(
            new InputStreamReader((System.in)));
        command = -1;
    }

    private void createList() throws IOException {
        for (int i = 0; i < 5; i++) {           
            System.out.format("Enter Show Name :");
            String name = input.readLine();
            System.out.format("Enter Time of the Show : ");
            int time = Integer.parseInt(input.readLine());
            ShowInfo show = new ShowInfo(name, time);
            showList.add(show);
        }
    }

    private void performTask() {
        try {
            createList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.format("How would you like to sort : %n");
        System.out.format("Press 0 : By Name%n");
        System.out.format("Press 1 : By Time%n");
        try {
            command = Integer.parseInt(input.readLine());
        } catch (Exception e) {
            e.printStackTrace();
        }
        sortList(showList);
    }

    private void sortList(List<ShowInfo> showList) {
        int min;
        for (int i = 0; i < showList.size(); i++) {

            // Assume first element is min
            min = i;
            for (int j = i+1; j < showList.size(); j++) {
                showList.get(j).setValues(command);
                int value = showList.get(j).compareTo(showList.get(min));
                if (value < 0) {
                    min = j;
                }
            }

            if (min != i) {
                Collections.swap(showList, i, min);
            }
        }
        System.out.println("TV Shows by Time");
        for(int i = 0; i < showList.size(); i++) {
            System.out.println(showList.get(i).getName() +
                " - " + showList.get(i).getTime());
        }
    }   

    public static void main(String[] args) {
        new Sorter().performTask();
    }
}

class ShowInfo  implements Comparable<ShowInfo> {

    private String name;
    private int time;
    private int command;

    public ShowInfo(String n, int t) {
        name = n;
        time = t;
    }

    public String getName() {
        return name;
    }

    public int getTime() {
        return time;
    }

    public void setValues(int cmd) {
        command = cmd;
    }

    public int compareTo(ShowInfo show) {
        int lastCmp = 1;
        if (command == 0) {
            lastCmp = name.compareTo(show.name);
        } else if (command == 1) {
            if (time < show.time) {
                lastCmp = -1;
            } else if (time == show.time) {
                lastCmp = 0;
            } else if (time > show.time) {
                lastCmp = 1;
            }
        }

        return lastCmp;
    }
}
于 2013-09-01T10:13:58.370 回答
1

使用易于实现的选择排序算法,

for (int i = 0; i < arr.length; i++)
        {
            for (int j = i + 1; j < arr.length; j++)
            {
                if (arr[i].time > arr[j].time) // Here ur code that which should be compare
                {
                    ShowInfo temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

无需检查 min 元素。浏览这个 wiki http://en.wikipedia.org/wiki/Selection_sort

于 2013-09-01T10:17:59.123 回答