목록 A가 목록 B와 완전히 똑같이 필요하다고 가정 해 봅시다. B가 가지고 있지만 A에 추가 할 필요가없는 모든 객체는 A가 있지만 B가 아닌 모든 객체는 있어야합니다. A에서 제거했습니다.한 목록을 다른 목록과 동일하게 만드는 효율적인 알고리즘은 무엇입니까?
내가 이것을 필요로하는 이유는 파일에 저장하는 플레이어의 ArrayList가 있기 때문입니다. Player의 속성을 업데이트 할 때마다 Players의 ArrayList를보고 저장하는 메서드를 호출하여 변경 내용을 파일에 저장합니다. 이 작업은 ArrayList에 Players에 대한 참조가 있기 때문에 가능합니다.
그러나 목록에서 플레이어를 검색 할 때마다 먼저 저장된 파일을 읽음으로써 목록을 업데이트합니다. 이렇게하면 모든 참조가 완전히 새로운 객체로 바뀝니다. 이렇게 한 후에 이전에 가져온 사용자를 변경하고 저장하려고하면 플레이어의 새 인스턴스가 변경된 인스턴스 대신 저장됩니다.
리스트를 다른 솔루션과 동일하게 만드는 좋은 알고리즘이 생길까요? 또는 거기에 참조를 유지하면서 전체 목록을 업데이트하는 더 좋은 방법이 있습니까?
업데이트 : 업데이트 된 솔루션은 O (nlogm) 시간으로 실행됩니다. 대상의 각 요소를 반복하고 소스에서 해당 요소를 검색합니다. 발견되면 소스에서 제거됩니다. 그렇지 않은 경우 대상에서 제거됩니다. 그런 다음 소스의 나머지 요소를 목적지에 추가하십시오. 목록을 물론 정렬해야하지만 파일에서 가져온 목록은 내가 추가 할 때 정렬되기 때문에 이미 정렬됩니다.
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CopyList {
public static void copyList(List dest, List src) {
copy(dest, src);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
dest.addAll(src);
}
public static void copyList(List dest, List src, Verify v) {
copy(dest, src);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
addAll(dest, src, v);
}
public static void copyList(List dest, List src, Comparator c) {
copy(dest, src, c);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
dest.addAll(src);
}
public static void copyList(List dest, List src, Comparator c, Verify v) {
copy(dest, src, c);
// the remaining elements in src list will be those that were originally
// in src but not in dest and so they need to be added
addAll(dest, src, v);
}
private static void copy(List dest, List src) {
// go through dest list to search if every element is in the new list
// travel backwards through dest because we will be removing elements from it
for(int i = dest.size()-1; i >= 0 ; i--) {
int src_i = Collections.binarySearch(src, dest.get(i));
if(src_i >= 0)
// if element is found in src list, remove it from src list
src.remove(src_i);
else
// if element is NOT found in src list, remove it from dest list
dest.remove(i);
}
}
private static void copy(List dest, List src, Comparator c) {
// go through dest list to search if every element is in the new list
// travel backwards through dest because elements might be removed
for(int i = dest.size()-1; i >= 0 ; i--) {
int src_i = Collections.binarySearch(src, dest.get(i), c);
if(src_i >= 0)
// if element is found in src list, remove it from src list
src.remove(src_i);
else
// if element is NOT found in src list, remove it from dest list
dest.remove(i);
}
}
private static void addAll(List dest, List src, Verify v) {
// verify each element in src list before adding it to dest list
for(Object o: src)
if(v.verify(o))
dest.add(o);
}
}
에 동의하셨습니까? 사용자 = new_users의 문제점은 무엇입니까? –
그건 OP가 요구 한 것이 아닙니다. 그들은 서로 다른 두 개의 목록을 원했습니다. –
java.util.Collections.copy (목록 super T> dest, List extends T> src) –