2014-04-09 8 views
-1

오늘 저는 어제보다 훨씬 어리 석한 문제가 있습니다. 그러나 왜 내 코드가 작동하지 않는지 이해할 수 없습니다.List 및 ArrayList 제거 메서드가 작동하지 않습니다.

public class A{ 
    List fileList; 

    public A(List fl){ 
     fileList = new ArrayList<String>; 
     if(fl != null) { 
      fileList = fl; 
     } 
    } 

    public void removeFiles(List fl){ 
     for(Object obj : fl) { 
      fileList.remove(obj.toString()); 
     } 
    } 
} 

나는 심지어 Iterator와 내가 웹에서 발견 된 모든 하나의 일을 시도하지만 파일이 제거되지 않은 및 오류 또는 예외가 온다 :

코드는 다음과 같습니다!

다른 사람 나는이 방법으로이 문제를 해결 한 경우 를 해결 좀 더 코드

public class CoordinationServer extends Activatable implements CoordinationSerInt 
{ 
List<CoordObj> userList; 
public int updateFileList(String username, List files, int option) throws RemoteException{ 
    for (CoordObj anUserList : userList) { 
     if (anUserList.getUsername().equals(username)) { 
      if(option == 0){ 
       anUserList.setFileList(files); 
       return 1; 

      }else if(option == 1){ 
       anUserList.addFilesToList(files); 
       return 1; 

      }else if(option == 2){ 
       anUserList.removeFiles(files); 
       return 1; 
      } 
      return 0; 
     } 
    } 
    return -1; 
} 
} 

public class CoordObj implements Serializable 
{ 
UserForUser userReference; 
List fileList; 
String username; 

public CoordObj(UserForUser ur, List fl, String un) { 
    userReference = ur; 
    fileList = new ArrayList<String>(); 
    if(fl != null) 
    fileList = fl; 
    username = un; 
} 

public void removeFiles(List fl){ 
    this.fileList.removeAll(fl); 
} 
} 

:

fileList.removeAll(Arrays.asList(fl.toArray())); 
+0

당신은'fileList.removeAll (FL)'도 아닌 구현 자신의 루프를 사용할 수 있어야합니다. [here] (http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#removeAll (java.util.Collection)) 설명서를 참조하십시오. – Phylogenesis

+0

@Phylogenesis : 또는'fileList.clear()'. – Brian

+0

아직 아무것도 ... fileList.removeAll (fl)을 시도했지만 아무 것도 제거하지 못했습니다 .. – Irith

답변

9

당신이 toString 개체를 요구하고있다() 방법, 그것을하지 마십시오. object itself을 사용하면됩니다.

for(Object obj:fl){ 
    fileList.remove(obj); 
} 

그러면 목록 btw에서 모든 항목이 삭제되므로 clear()을 수행 할 수 있습니다.

+0

이미 말한 것을 시도했지만 어쨌든 작동하지 않습니다 ... – Irith

+0

@ Leeith 분명히 작동합니다 – NimChimpsky

+0

clear() 또는 전체 fileList가 지워지지 않습니다! 파일 목록에있는 fl 파일도 삭제하고 싶습니다. – Irith

3

이것은 오류 또는 예외를 발생하지 않습니다

fileList = new ArrayList<String>(); 
+0

당신은 쓰고 있습니다, 나는 방금 잘못을 복사했지만 죄송합니다. .. – Irith

0

fileList = new ArrayList<String>; 

변경이 잘못된 것입니다. API documentation에 따라 ClassCastException을 던지는 것은 선택 사항이며 List은 던지지 않습니다. 또한 "..이 목록에 요소가 포함되어 있지 않으면 변경되지 않습니다 ...". 따라서 작업은 본질적으로 아무 작업이 아닙니다.

fileList.removeAll(fl)을 사용하여 fileList에서 제거 할 항목을 모두 제거하십시오. 루프가 필요 없습니다. removeAllfileList이 변경된 경우 true을 반환하므로 작업이 성공했는지 여부를 확인할 수 있습니다.

+0

'List fl' 쉘 인수와 경합하는 요소 만 삭제됩니다. – ifloop

+0

@ifLoop : 그런 다음'removeAll'을 사용하십시오. – Brian

+0

내가 말했듯이 removeAll도 작동하지 않습니다 ... 정말 이해할 수 없습니다 .. – Irith

0

당신이 String 유형 목록을 선언하면서, 당신은 다만 할 수 있습니다

fileList = new ArrayList<String>(); 

for(String str : fl){ 
     fileList.remove(str); 
} 
관련 문제