Java 如何比较Int´;在ArrayList和ArrayList之间是什么?(爪哇)

Java 如何比较Int´;在ArrayList和ArrayList之间是什么?(爪哇),java,arraylist,Java,Arraylist,我现在正在给日历编代码。我将约会保存在另一个ArrayList中的两个不同ArrayList中 字符串(主题、地点、人物)进入另一个ArrayList中的第一个ArrayList=arStr 整数(日期和时间)进入另一个ArrayList=arInt中的第二个ArrayList 创建约会时,我希望根据日期对其进行排序。因此,如果我想添加新约会,它应该保存在outter列表中已保存约会的上方或下方(取决于时间)。如果已保存的日期晚于新的日期,则应将其放在outter列表中。完成后,我想将字符串约会

我现在正在给日历编代码。我将约会保存在另一个ArrayList中的两个不同ArrayList中

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

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

我的问题是,我找不到一种方法,以这种方式来排序,谁能帮我:)

公共类日历
{
public static ArrayList arInt=new ArrayList();
public static ArrayList arStr=new ArrayList();
公共静态扫描仪读取=新扫描仪(System.in);
public static int counter=0;//统计所有预约
公共静态void main(字符串[]args)
{
//为空间添加ArryList
add(newarraylist());
添加(新的ArrayList());
add(newarraylist());
添加(新的ArrayList());
//这是一个约会(主题、年、月、日、小时)
//已保存约会
计数器++;
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);
//新任命
计数器++;
字符串betreff=“1.约会”;
国际年=2017年;
整月=2;
整日=8;
整小时=15;
//如何将这些变量与第一次约会进行比较并相应地保存?
}
}

我的建议是创建新类
约会
,为其分配
LocalDateTime
,并让日历存储该类型的列表,例如
列表。

我的建议是创建新类
约会
,为其分配
LocalDateTime
,并让日历存储该类型的列表,例如
List.

首先,在其他
ArrayList
s中有
ArrayList
s,在本例中完全不需要这些。删除这些选项将大大简化您的代码:

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

    public static void main (String[]args)
    {
        // This is one Appointment ( Subject, Year, Month, Day, Hour )
        // Already saved Appointment
        counter++;
        arStr.add("3.Liste");
        arInt.add(2017);
        arInt.add(2);
        arInt.add(8);
        arInt.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 ?
    }
}
然后,您将能够创建以下内容的ArrayList:

ArrayList<Appointment> appointments = new ArrayList<>();
您需要编写自己的“比较器”来实现这一点。这基本上是一个比较两个对象以确定哪一个先到的函数。一月一日在一月三日之前吗?那种事


有关更多信息,请参阅或谷歌“编写自定义比较器java”。

首先,在其他
ArrayList
s中有
ArrayList
s,在本例中完全没有必要。删除这些选项将大大简化您的代码:

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

    public static void main (String[]args)
    {
        // This is one Appointment ( Subject, Year, Month, Day, Hour )
        // Already saved Appointment
        counter++;
        arStr.add("3.Liste");
        arInt.add(2017);
        arInt.add(2);
        arInt.add(8);
        arInt.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 ?
    }
}
然后,您将能够创建以下内容的ArrayList:

ArrayList<Appointment> appointments = new ArrayList<>();
您需要编写自己的“比较器”来实现这一点。这基本上是一个比较两个对象以确定哪一个先到的函数。一月一日在一月三日之前吗?那种事


有关更多信息,请参阅或谷歌“编写自定义比较器java”。

那么基本上我只需要一个由约会组成的ArrayList?谢谢你的帮助!只是一个建议。当然,一个真正成熟的日历实现可能要复杂得多;但是如果你的任务是存储一些约会,是的,我会这样做。它不知道这是否会改变什么,但所有的操作都包括删除约会,更改约会,是的,就是这样:'基本上我只会有一个由约会组成的ArrayList吗?谢谢你的帮助!只是一个建议。当然,一个真正成熟的日历实现可能要复杂得多;但是如果你的任务是存储一些约会,是的,我会这样做。它不知道这是否会改变什么,但所有的操作包括删除约会、更改约会,是的,就是这样:'这真的很有帮助,谢谢你。但是我不明白一件事,如果让我们假设所有的日期和时间都保存在一个ArrayListInt中,我如何才能将它们排序到正确的约会?Thx需要您的帮助(Y)将不会有
ArrayListInt
。您将创建一个名为约会的
ArrayList
。您将在不同的
约会
对象中插入:
约会。添加(新约会(“我的名字”,somedate)
。然后按您的意愿排序。哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦哦没有
ArrayListInt
。您可以创建一个名为
Appointment
ArrayList
。您可以在不同的
Appointment
对象中插入:
Appointment。添加(新约会(“我的名字”,somedate)
。然后根据需要对其进行排序。Oooohohhhhhhhhhhhhhhhhhhhhhhhhhh,谢谢!
Collections.sort(appointments, aCustomComparator);