如何在ArrayLists中比较ArrayLists之间的Int? (Java)的

时间:2017-01-06 14:19:05

标签: java arraylist

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

字符串(Subject,Place,People)进入另一个ArrayList = arStr中的第一个ArrayList 整数(日期和时间)进入另一个ArrayList = arInt

中的第二个ArrayList

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

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

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 ?

    }
}

2 个答案:

答案 0 :(得分:4)

我的建议是创建新课程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

答案 1 :(得分:0)

首先,您在其他ArrayList内有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 ?
    }
}

下一步,您需要统一这两个列表。您目前拥有的是一个日期列表和一个单独的约会名称列表。您需要一个约会列表。要做到这一点,你需要写一个约会课:

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'。