2013-06-05 4 views
0

arraylist에 객체를 추가 한 다음 ID로 정렬하는 프로그램을 작성했습니다.arraylist에 객체 추가 및 정렬

package TestPackage; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Scanner; 

import com.fulcrum.emp.EmployeeSortById; 

public class EmployeeSorting { 
    // path till 'employee files' folder. 
    File folder = new File("D:\\Arthi iyer\\employee files"); 

    // listFiles() : list all the files in a folder. 
    File[] listOfFiles = folder.listFiles(); 

    ArrayList<Employee> emp = new ArrayList<Employee>(); 
    String[] split_input = null; 

    public void sortFiles() throws FileNotFoundException { 

     for (File file : listOfFiles) { 
      Scanner scanner = new Scanner(file); 
      String input = scanner.nextLine(); 
      split_input = input.split("="); 
      int id = Integer.parseInt(split_input[1]); 

      String input1 = scanner.nextLine(); 
      split_input = input1.split("="); 
      String name = split_input[1]; 

      String input2 = scanner.nextLine(); 
      split_input = input2.split("="); 
      int age = Integer.parseInt(split_input[1]); 

      // Employee e=new Employee(id, name, age); 
      // System.out.println(e); 
      emp.add(new Employee(id, name, age)); 
      // System.out.println(emp.size()); 

      // for(int i=0;i<emp.size();i++) 
      // { 
      // System.out.println(emp.get(i) +""+i); 
      // } 

     }// for ends 
    }// method ends 

    public void sortByID() { 

     System.out.println("----Sort By Employee Id----"); 
     Collections.sort(emp, new EmployeeSortById()); 
    } 

    public static void main(String[] args) throws FileNotFoundException { 

     EmployeeSorting sort = new EmployeeSorting(); 
     sort.sortFiles(); 
     sort.sortByID(); 

    } 

} 

내 문제는 그것이 말하는 오류를 준다 : (ArrayList를 EmployeeSortById)

유형의 컬렉션에있어서, 정렬 (목록, 비교기)를 인수 적용되지 않습니다

여기 내 프로그램입니다

EmployeeSortById 클래스에서 나는 Comparator<Employee>을 올바르게 구현했습니다. 아직도 문제가 있습니다. 아무도 나를 안내 해줄 수 있습니까?

+0

이 EmployeeSortById' '의 구현을 제공하시기 바랍니다 사용하기 전에 EmployeeSortById를 정의 할 필요가있다. –

+0

내 문제가 해결 됐어. 바보 같은 실수였다. 내가 다른 파일 com.fulcrum 패키지에서이 코드를 붙여 넣을 때 가져온 코드가 문제가된다. : D .. 실행 중이다. .anyways에 대한 도움이 :) – user2412380

+0

@ user2412380 - 나는 직원 정보를 저장하기 위해 hashmap을 사용하는 것이 더 좋다고 생각합니다. 거기에 어떤 문제가 있습니까? –

답변

0

당신은

public class EmployeeSortById implements Comparator{ 

public int compare(Employee o1, Employee o2) { 
    Employee p1 = (Employee) o; 
    Employee p2 = (Employee) o; 
    return p1.getEmployeeId() - p2.getEmployeeId(); 
}} 

public void sortByID() { 

    System.out.println("----Sort By Employee Id----"); 
    Collections.sort(emp,EmployeeSortById); 
} 
+0

대답을 제공하는 대신 ** OP 코드를 검토해 보시지 않겠습니까? (힌트 : OP가 자신의 코드를 업데이트 할 때까지 기다리 자.) Geez ... –

+0

@LuiggiMendoza 고맙습니다. Ops code – PSR

+0

@LuiggiMendoza ok ok 나는 이해합니다. 죄송합니다. – PSR