【问题标题】:How to compare Int´s between ArrayLists whithin ArrayLists ? (java)如何比较 ArrayLists 和 ArrayLists 之间的 Int? (爪哇)
【发布时间】:2017-05-21 07:57:49
【问题描述】:

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

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

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

我的问题是我找不到以这种方式对它们进行排序的方法,谁能帮助我:)?

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 ?

    }
}

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

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

    之后,使用内置的排序方法和您自己的比较器很容易对约会进行排序:

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

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

    【讨论】:

    • 所以基本上我只需要一个由 Appointments 组成的 ArrayList 吗?感谢您的帮助!
    • 只是一个建议。当然,真正成熟的 Calendar 实现可能要复杂得多。但如果你的任务是存储一些约会,是的,我会这样做。
    • 它不知道这是否会改变任何事情,但所有操作都包括删除约会、更改约会等等:'D
    【解决方案2】:

    首先,您在其他ArrayLists 中有ArrayLists,在这种情况下完全没有必要。删除这些会大大简化您的代码:

    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 ?
        }
    }
    

    下一步,您需要统一这两个列表。您目前拥有的是一份日期列表和一份单独的约会名称列表。您需要一个约会列表。为此,您需要编写一个 Appointment 类:

    public class Appointment
    {
        private String name;
        private Date   date;
    
        // and so on
    }
    

    然后您将能够创建一个 ArrayList:

    ArrayList<Appointment> appointments = new ArrayList<>();
    

    为了能够以您可以使用的各种方式对列表进行排序:

    Collections.sort(appointments, aCustomComparator);
    

    您需要编写自己的“比较器”来完成此操作。这基本上是一个比较两个对象以查看哪个对象先出现的函数。 1 月 1 日会早于 1 月 3 日吗?那种东西。

    有关详细信息,请参阅 this answer 或 Google '编写自定义比较器 java'。

    【讨论】:

    • 这真的很有帮助,谢谢。但是我不明白一件事,如果让我们说所有日期和时间都保存在一个 ArrayListInt 中,我将如何将它们排序到正确的约会?谢谢你的帮助(Y)
    • 不会有ArrayListInt。您将创建一个名为appointmentsArrayList&lt;Appointment&gt;。您将插入不同的Appointment 对象:appointments.add(new Appointment("my name", somedate)。然后根据需要对其进行排序。
    • oooohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2017-07-26
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多