gpt4 book ai didi

java - 对链表方法内的日期进行排序

转载 作者:行者123 更新时间:2023-11-30 03:08:07 29 4
gpt4 key购买 nike

我在待办事项列表 list2016 中添加了一堆日期,现在我需要按日期对这些待办事项列表项进行排序(最早的在前,最新的在最后)。

这一切都应该发生在我的 add 方法中,但我不知道如何做到这一点。有什么想法吗?

public class ToDoListEntry 
{
String task;
LocalDate date;
ToDoListEntry next;

public ToDoListEntry(LocalDate date, String task)
{
this.task = task;
this.date = date;
}
}

接下来我尝试添加一个数组,但没有成功:

public class ToDoList 
{
ToDoListEntry first;
int size;

public ToDoList ()
{
first = null;
size = 0;
}

public void add(ToDoListEntry newTask)
{
if (first == null)
{
first = newTask;
}
else
{
ToDoListEntry zeiger = first;
while (zeiger.next != null)
{
zeiger = zeiger.next;
}
zeiger.next = newTask;
}
size++;
}

public String print()
{
String result = "";
if (first == null)
{
result = "Empty list!\n";
}
else
{
ToDoListEntry pointer = first;
while (pointer != null)
{
result += "Until " + pointer.date + " Task: " + pointer.task + "\n";
pointer = pointer.next;
}
}
System.out.println(result);
return result;
}
}

最后是主类。它应该创建一个新的 ToDoList 并将其打印出来(请注意,我没有包含 print() 方法):

public class MyMainClass
{
public static void main(String[] args)
{
System.out.println("Test 00: Empty List");
ToDoList list2016 = new ToDoList();

list2016.print();

System.out.println("Test 01: add");
list2016.add(new ToDoListEntry(LocalDate.of(2016, 8, 15), "Do workout"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 6, 3), "Buy apples"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Books"));
list2016.print();
}
}

最佳答案

这是一种低效且困惑的方式。然而,为了实现你想要的,我改变了你的两个类(class)。这些类是您的 ToDoList 类和 main 类。

ToDoList.java

import java.util.*;
import java.time.*;

public class ToDoList
{
ToDoListEntry first;
ArrayList<LocalDate> datesList = new ArrayList();
ArrayList<String> results = new ArrayList();

public ToDoList ()
{
first = null;
}

public void add(ToDoListEntry newTask)
{
if (first == null)
{
first = newTask;
}
else
{
ToDoListEntry zeiger = first;
while (zeiger.next != null)
{
zeiger = zeiger.next;
}
zeiger.next = newTask;
}
}

public void sortDates()
{
Collections.sort(datesList);
}

public void compareAndSort()
{
ArrayList<String> tempDates = new ArrayList();
for(LocalDate lD:datesList)
{
tempDates.add(lD+"");
}
ArrayList<String> tempSorted = new ArrayList();
for(String s:tempDates)
{
for(String a:results)
{
if(a.contains(s))
{
if(!tempSorted.contains(a))
{
tempSorted.add(a);
}
}
}
}

results=tempSorted;
for(String s:results)
{
System.out.println(s);
}
}

public String print()
{
String result = "";
if (first == null)
{
result = "Empty list!\n";
System.out.println(result);
}
else
{
ToDoListEntry pointer = first;
while (pointer != null)
{
result += "Until " + pointer.date + " Task: "+ pointer.task +"\n";
if(LocalDate.now().isBefore(pointer.date))
{
results.add("Until " + pointer.date + " Task: "+ pointer.task);
}

datesList.add(pointer.date);
pointer = pointer.next;
}
}
return result;
}

public String printDates()
{
String dates = datesList.toString();
System.out.println(dates);
return dates;
}
}

Test.java(这就是您所描述的main类)

import java.util.*;
import java.time.*;

import java.util.*;
import java.time.*;

public class Test
{
public static void main(String[] args)
{
System.out.println("Test 00: Empty List");
ToDoList list2016 = new ToDoList();

list2016.print();

System.out.println("Test 01: add");
list2016.add(new ToDoListEntry(LocalDate.of(2016, 8, 15), "Do workout"));
list2016.add(new ToDoListEntry(LocalDate.of(2015, 2, 18), "Hand in Homework"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 6, 3), "Buy apples"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Books"));
list2016.add(new ToDoListEntry(LocalDate.of(2016, 10, 11), "Read Homework"));

list2016.print();
list2016.sortDates();
list2016.compareAndSort();
}
}

我有两个笔记。

第一个是因为您特别说过最快,我添加了一个函数来删除已经超过当前日期的日期。如果您愿意,稍后可以通过更改 if 语句来更改此设置 if(LocalDate.now().isBefore(pointer.date))

其次,除非 first 为 null,否则您的 ToDoList.print() 实际上不再打印任何内容。

关于java - 对链表方法内的日期进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253027/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com