2017-01-06 5 views
0

저는 현재 캘린더를 코딩하려고합니다. 나는 다른 ArrayList 내에 2 개의 다른 ArrayList에 내 Appointments를 저장합니다.ArrayLists 사이의 Int를 비교하는 방법 ArrayLists?

문자열 (주제, 장소, 사람) 내가 약속 I를 만들 = arInt

다른 ArrayList를 내 두 번째의 ArrayList에 가서 다른 ArrayList를 = arStr
정수 (날짜 및 시간) 내에서 첫 번째의 ArrayList로 이동 날짜별로 정렬하고 싶습니다. 따라서 새로운 약속을 추가하고 싶으면, 그것을 외래 목록에있는 저장된 것의 위나 아래에 저장해야합니다 (시간이 지날수록). 날짜가 새 것보다 늦으면 이미 저장된 사람들은 outter List에서 내려야합니다. 그 후에는 Int 약속에 문자열 약속을 연결하고 싶습니다.

내 문제는 내가 이런 식으로 그들을 정렬 할 수있는 방법을 찾을 수있다, 아무도 나를 도와 줄 수 pls :)?

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 ? 

    } 
} 

답변

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.

+0

기본적으로 나는 단지 하나의 ArrayList thats가 Appointments로 구성되어 있을까요? Thx 당신의 도움! – Coldvirus

+0

그냥 제안. 물론 실제 실제 Calendar 구현은 훨씬 더 복잡 할 수 있습니다. 그러나 당신의 업무가 몇몇 약속을 저장하는 것이라면, 그렇습니다, 나는 그렇게 할 것입니다. – Halmackenreuter

+0

변경 사항이 있는지는 모르겠지만 모든 작업에는 약속 삭제, 약속 변경, 예 : 'D – Coldvirus

0

먼저 ArrayList 초 안에 ArrayList 개를 입력해야합니다.이 경우에는 전혀 필요하지 않습니다. 코드를 제거하면 코드가 크게 단순화됩니다.

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 '맞춤형 비교기 작성'을 참조하십시오.

+0

그게 정말 도움이됩니다. 감사합니다.그러나 모든 일과 시간이 하나의 ArrayListInt에 저장된다고 말하면, 내가 어떻게 올바른 Appointment로 정렬 할 수 있습니까? 당신의 도움을위한 Thx (Y) – Coldvirus

+0

'ArrayListInt'는 없을 것입니다. 'appointments'라는 하나의'ArrayList '을 생성 할 것입니다. 'Appointment'라는 다른 객체에'appointments.add (새 약속 (내 이름), somedate)'라고 쓰면됩니다. 그럼 원하는대로 정렬하십시오. – Michael

+0

oooohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh, 고마워요! – Coldvirus