gpt4 book ai didi

java - 如何比较 ArrayList 中的 ArrayList 之间的 Int? (java)

转载 作者:行者123 更新时间:2023-12-01 18:00:17 24 4
gpt4 key购买 nike

我目前正在尝试编写日历代码。我将约会保存在另一个 ArrayList 中的 2 个不同的 ArrayList 中。

字符串(主题、地点、人物)进入另一个 ArrayList 中的第一个 ArrayList = arStr
整数(日期和时间)进入另一个 ArrayList 中的第二个 ArrayList = arInt

当我创建约会时,我想根据日期对其进行排序。因此,如果我想添加一个新的约会,它应该保存在外部列表中已保存约会的上方或下方(取决于时间)。如果已保存的日期晚于新日期,则应将其记录在外部列表中。完成后,我想将字符串约会连接到整数约会。

我的问题是我找不到以这种方式对它们进行排序的方法,任何人都可以帮助我吗:)?

public class Calender
{
public static ArrayList<ArrayList<Integer>> arInt = new ArrayList<ArrayList<Integer>>();
public static ArrayList<ArrayList<String>> arStr = new ArrayList<ArrayList<String>>();
public static Scanner read = new Scanner(System.in);
public static int counter = 0; // counts all made appointments

public static void main (String[]args)
{
//Adding Arrylists for space
arInt.add(new ArrayList());
arStr.add(new ArrayList());
arInt.add(new ArrayList());
arStr.add(new ArrayList());

// This is one Appointment ( Subject, Year, Month, Day, Hour )
// Already saved Appointment
counter++;
arStr.get(0).add("3.Liste");
arInt.get(0).add(2017);
arInt.get(0).add(2);
arInt.get(0).add(8);
arInt.get(0).add(16);

// new Appointment
counter++;
String betreff = "1. Appointment";
int year = 2017;
int month = 2;
int day = 8;
int hours = 15;

// How to compare these Variables with the first Appointment and save it accordigly ?

}
}

最佳答案

我的建议是创建新类 Appointment ,为其分配 LocalDateTime并让日历存储该类型的列表,例如List<Appointment> appointments = new ArrayList<>() .

之后,可以使用内置排序方法和您自己的比较器轻松对约会进行排序:

appointments.sort((a1, a2) -> a1.date.isBefore(a2.date));)

无论如何,我建议你先做一个面向对象设计的教程,比如https://www.tutorialspoint.com/object_oriented_analysis_design/index.htm .

关于java - 如何比较 ArrayList 中的 ArrayList 之间的 Int? (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41507659/

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