ArrayList에서 항목 제거
나는 ArrayList
가정 list
하고 8 개의 항목 AH를 가지고 있으며 이제 list
어떻게 할 수 있는지 에서 int 배열에 저장된 1,3,5 위치 항목을 삭제하고 싶습니다 .
나는 이것을 시도하고있다
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");
list.add("H");
int i[] = {1,3,5};
for (int j = 0; j < i.length; j++) {
list.remove(i[j]);
}
그러나 첫 번째 항목 삭제 후 배열의 위치가 변경되고 다음 반복에서 잘못된 요소를 삭제하거나 예외가 발생합니다.
이 경우 내림차순으로 요소를 제거해야합니다. 첫 번째 인덱스 5
, 다음 3
, 다음 1
. 이렇게하면 원하지 않는 부작용없이 목록에서 요소가 제거됩니다.
for (int j = i.length-1; j >= 0; j--) {
list.remove(i[j]);
}
, ArrayList
사용 ListIterator
에서 요소를 제거 할 수 있습니다 .
ListIterator listIterator = List_Of_Array.listIterator();
/* Use void remove() method of ListIterator to remove an element from List.
It removes the last element returned by next or previous methods.
*/
listIterator.next();
//remove element returned by last next method
listIterator.remove();//remove element at 1st position
listIterator.next();
listIterator.next();
listIterator.remove();//remove element at 3rd position
listIterator.next();
listIterator.next();
listIterator.remove();//remove element at 5th position
public void DeleteUserIMP(UserIMP useriamp) {
synchronized (ListUserIMP) {
if (ListUserIMP.isEmpty()) {
System.out.println("user is empty");
} else {
Iterator<UserIMP> it = ListUserIMP.iterator();
while (it.hasNext()) {
UserIMP user = it.next();
if (useriamp.getMoblieNumber().equals(user.getMoblieNumber())) {
it.remove();
System.out.println("remove it");
}
}
// ListUserIMP.remove(useriamp);
System.out.println(" this user removed");
}
Constants.RESULT_FOR_REGISTRATION = Constants.MESSAGE_OK;
// System.out.println("This user Deleted " + Constants.MESSAGE_OK);
}
}
전에 언급했듯이
iterator.remove()
루프 중에 목록 항목을 제거하는 유일한 안전한 방법 일 수 있습니다.
For deeper understanding of items removal using the iterator, try to look at this thread
I assume the array i is ascend sorted, here is another solution with Iterator, it is more generic:
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");
list.add("H");
int i[] = {1,3,5};
Iterator<String> itr = list.iterator();
int pos = 0;
int index = 0;
while( itr.hasNext() ){
itr.next();
if( pos >= i.length ){
break;
}
if( i[pos] == index ){
itr.remove();
pos++;
}
index++;
}
How about this? Just give it a thought-
import java.util.ArrayList;
class Solution
{
public static void main (String[] args){
ArrayList<String> List_Of_Array = new ArrayList<String>();
List_Of_Array.add("A");
List_Of_Array.add("B");
List_Of_Array.add("C");
List_Of_Array.add("D");
List_Of_Array.add("E");
List_Of_Array.add("F");
List_Of_Array.add("G");
List_Of_Array.add("H");
int i[] = {1,3,5};
for (int j = 0; j < i.length; j++) {
List_Of_Array.remove(i[j]-j);
}
System.out.println(List_Of_Array);
}
}
And the output was-
[A, C, E, G, H]
Try it this way,
ArrayList<String> List_Of_Array = new ArrayList<String>();
List_Of_Array.add("A");
List_Of_Array.add("B");
List_Of_Array.add("C");
List_Of_Array.add("D");
List_Of_Array.add("E");
List_Of_Array.add("F");
List_Of_Array.add("G");
List_Of_Array.add("H");
int i[] = {5,3,1};
for (int j = 0; j < i.length; j++) {
List_Of_Array.remove(i[j]);
}
String[] mString = new String[] {"B", "D", "F"};
for (int j = 0; j < mString.length-1; j++) {
List_Of_Array.remove(mString[j]);
}
remove(int index) method of arraylist removes the element at the specified position(index) in the list. After removing arraylist items shifts any subsequent elements to the left.
Means if a arraylist contains {20,15,30,40}
I have called the method: arraylist.remove(1)
then the data 15 will be deleted and 30 & 40 these two items will be left shifted by 1.
For this reason you have to delete higher index item of arraylist first.
So..for your given situation..the code will be..
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");
list.add("H");
int i[] = {1,3,5};
for (int j = i.length-1; j >= 0; j--) {
list.remove(i[j]);
}
If you use "=", a replica is created for the original arraylist in the second one, but the reference is same so if you change in one list , the other one will also get modified. Use this instead of "="
List_Of_Array1.addAll(List_Of_Array);
참고URL : https://stackoverflow.com/questions/10714233/remove-item-from-arraylist
'program story' 카테고리의 다른 글
Rails에서 속성의 원래 값을 얻는 방법 (0) | 2020.08.26 |
---|---|
MongoDB : 필드가 null이거나 설정되지 않은 레코드를 쿼리하는 방법은 무엇입니까? (0) | 2020.08.25 |
로컬 파일을 덮어 쓰지 않고 원격에서 파일을 가져 오려면 어떻게해야합니까? (0) | 2020.08.25 |
영구 PowerShell 별칭을 만드는 방법 (0) | 2020.08.25 |
XAML에 유니 코드 문자를 넣는 방법은 무엇입니까? (0) | 2020.08.25 |