2017-03-31 57 views
2

我正在为java类项目制作一个待办事项列表。在jList中,我显示一个日期和一个表示日期的字符串,以及当天需要完成的工作。我也希望能够按照它们的日期对这些事件进行排序,因此应尽早出现的任务首先出现。我知道如何使用冒泡排序对日期列表进行排序,但我不知道如何将它应用到我的列表中,因为每个项目都包含日期和字符串。这是迄今为止的代码。有谁知道我将如何按顺序对日期进行排序,并将字符串附加到日期中。谢谢。使用多个元素对数组进行排序

public class ToDoListManager { 

    final int maxitems = 20; 
    int items = 0; 
    String[] toDoList = new String[20]; 
    Date[] Dates = new Date[20]; 

    String addItem(String toDo, Date date) { 
     if (items == maxitems) { 
      return ("No more Space"); 
     } else if (toDo.equalsIgnoreCase("") || (toDo == null) || (date == null)) { 
      return ("All fields required"); 
     } else if (items < maxitems) { 
      Dates[items] = date; 
      SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd"); 
      toDoList[items] = "On " + dt1.format(date) + " " + toDo; 
      items = items + 1; 
      return (toDo + " Was Added to the List"); 
     } 
     return null; 
    } 

    void Order(Date[] dates) { 
     Date temp; 
     boolean fixed = false; 
     while (fixed == false) { 
      fixed = true; 
      for (int i = 0; i < dates.length - 1; i++) { 
       if (dates[i].before(dates[i + 1])) { 
        temp = dates[i + 1]; 
        dates[i + 1] = dates[i]; 
        dates[i] = temp; 
        fixed = false; 
       } 
      } 

     } 
     System.out.println(dates); 

    } 

    String[] getItems() { 
     String[] displayList = new String[items]; 
     for (int i = 0; i < items; i++) { 
      displayList[i] = toDoList[i]; 

     } 

     return displayList; 

    } 
} 
+1

考虑从创建代表您的任务的对象开始,该对象包含日期和字符串。 – njzk2

+2

请勿使用[平行集合](https://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/)。定义一个'ToDo'类。 – Michael

回答

1

您在问题中提到了JList,因此我假设您想要查看已排序的TODO。以下是一种可能的方法:

JList由ListModel<E>实例支持。这是一个通用类,可以包含E的实例列表。所以接线一起一个可行的办法是:

  • 创建一个代表在待办事项列表(类待办事项,具有包含如日期和TODO文本字段)一个项目的类。
  • 定义ToDo实例的一个或多个比较器 - 例如,一个基于日期,另一个基于文本,无论你需要什么。
  • 用您的ToDo实例填充一个List(或另一个集合),并使用其中一个比较器对其进行分类。
  • 创建一个由列表支持的ListModel实现,并将其设置为JList组件。 ListModel实现可以是例如AbstractListModel的扩展名。另请参阅example of sorted ListModel implementation
  • 要在列表中以有意义的方式显示ToDos,请在ToDo类上创建toString()实现(JList将调用该实现以获取对象的字符串表示形式),或者supply your own ListCellRenderer to the JList instance
  • 如果您需要重新排序,请确保您的ListModel实现在每次改变顺序时都正确地对待JList。
1

为什么不使用SortedMap的Date-> String>?

+0

这是要走的路 –

+1

为什么不使用类而不是[parallel collections](https://codeblog.jonskeet.uk/2014/06/03/anti-pattern-parallel-collections/)? – Michael

0

迈克尔提到的另一种做法是做一个对象。这将存储你想要的变量,日期和字符串。如果在未来要再添变数,你可以很容易地只添加此对象,而无需编辑列表,你的对象存储。

public class Todo { 

private Date date; 
private String info; 

public Todo(Date date, String info){ 
    this.date = date; 
    this.info = info; 
} 

public Date getDate() { 
    return date; 
} 
public void setDate(Date date) { 
    this.date = date; 
} 
public String getInfo() { 
    return info; 
} 
public void setInfo(String info) { 
    this.info = info; 
} 
} 
+0

[对象应该是不可变的](http://www.yegor256.com/2014/06/09/objects-should-be-immutable.html)。制定者没有理由。 – Michael

0

这大约是我将如何构建应用程序。为了简洁,我省略了ordergetItems方法。

public class ToDoListManager { 

    private static class ToDo 
    { 
     public final String text; 
     public final Date date; 
     public ToDo(String text, Date date) 
     { 
      this.text = text; 
      this.date = date; 
     } 
    } 

    final int maxItems = 20; 
    final List<ToDo> todos = new LinkedList<>(); 

    String addItem(String text, Date date) { 
     if (todos.size() == maxItems) { 
      return ("No more Space"); 
     } else if (text == null || text.equalsIgnoreCase("") || date == null) { 
      return ("All fields required"); 
     } else if (todos.size() < maxItems) { 

      final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
      text = "On " + format.format(date) + " " + text; 

      todos.add(new ToDo(text, date)); 

      return (text + " Was Added to the List"); 
     } 
     return null; 
    } 
} 

你可以看到事情变得简单多了。一旦你沟原始阵列赞成收藏工作,并开始使用类。


现在回答你的问题:不是实现自己的排序,可以使用Collections.sort(todos)

为了适当地对您的列表进行分类,您需要ToDo来实施Comparable<ToDo>。我会把这个留给你去研究和实施。

+0

谢谢。这有很大帮助。我现在遇到的麻烦是我不确定如何让jList显示新类ToDo的对象。 –

+0

我对'JList'不熟悉,但是如果你需要一个原始数组(即ToDo []),那么你可以调用'todos.toArray()'。 – Michael

+0

不,我使用需要一个字符串数组(String [])的jLists setListData方法。不确定如何获得列表来显示这个类,因为它似乎只想显示字符串数组。 –

相关问题