[英]Removing an element from ArrayList
I have an ArrayList
which contain 10 elements and a variable int = 12
. 我有一个
ArrayList
,其中包含10个元素和一个变量int = 12
。 Now I want to count how many elements are in array
and if are less than 12
to start to count again from 0 and stop to index 2 and remove it, until I will have one element in my array. 现在,我想计算
array
有多少个元素,如果少于12
,则要从0开始重新计数,然后停止到索引2并将其删除,直到数组中只有一个元素为止。 I tried the following: 我尝试了以下方法:
int j = 12;
int l = 0;
// Here I check if j is less than array.size
while (j < array.size()) {
for (int i = 0; i < array.size(); i++) {
if (j == i + 1) {
array.remove(i);
}
}
}
// Here is for j greater than array.size
while (array.size() != 1) {
for (int i = 0; i < array.size(); i++) {
l = j - array.size();
if (l < array.size()) {
array.remove(l);
}
}
}
System.out.println(array);
UPDATE: 更新:
MyArray = {1,2,3,4,5,6,7,8,9,10};
int=12;
MyArray
contain just 10
elements, but I want to delete the index with number 12
, as long as index 12
does not exist I should start to count again from zero, and the number 12
is at index 2
, That's why I should delete the index with number 2
. MyArray
仅包含10
元素,但是我想删除数字为12
的索引,只要索引12
不存在,我就应该从零开始重新计数,并且数字12
在索引2
,这就是为什么我应该删除索引与2
。 The second iteration MyArray
will contain just 9
elements, and again 12-9=3
, I should delete the index with number 3
, until I will have just one element in MyArray
第二次迭代
MyArray
将仅包含9
元素,再次12-9=3
,我应该删除编号为3
的索引,直到在MyArray
只有一个元素为止
Instead of looping twice through the array to remove the last n elements until the length of the list equals j, you could simply use: 您可以使用以下方法来代替遍历数组两次以删除最后的n个元素,直到列表的长度等于j,而不是:
while (j < array.size()) {
array.remove(j - 1);
}
If you always want to remove index 2, you could do: 如果您始终要删除索引2,则可以执行以下操作:
while (array.size() >= 3) { // otherwise you will get a ArrayIndexOutOfBoundsException
array.remove(2);
}
However, you will have two elements left in your ArrayList instead of 1 (at index 0 and 1). 但是,您将在ArrayList中保留两个元素,而不是1(在索引0和1)。 You cannot delete ìndex 2 at that point, because it is not a valid index any longer.
此时您无法删除“ ndex 2”,因为它不再是有效索引。
Thus, you could either remove index 0/1 afterwards or what I think you want to achieve: 因此,您可以在以后删除索引0/1或我想达到的目标:
while (array.size() >= 2) { // otherwise you will get a ArrayIndexOutOfBoundsException
array.remove(1);
}
Then only one element will remain in your list at index 0. 然后,列表中只有一个元素将保留在索引0处。
Edit: for the update of your question it is 编辑:对于您的问题的更新是
int originalSize = array.size();
while (array.size() >= originalSize - j) { // otherwise you will get a ArrayIndexOutOfBoundsException
array.remove(originalSize - j);
}
However, you will always be left with size - j
items in your list. 但是,您始终会剩下
size - j
列表中的size - j
项目。 You cannot remove index 3, for example, until you have only one element in your list. 例如,直到列表中只有一个元素,您才能删除索引3。
An answer to the updated question: 对更新问题的答案:
When you have a list of length 10 and you want to delete the "12th element" you can use the modulo operator: 当您有一个长度为10的列表并且想要删除“第12个元素”时,可以使用模运算符:
ArrayList<...> someList = ...;
int toDelete = 12;
int deleteInRange = toDelete % someList.size();
someList.remove(deleteInRange);
The modulo operator will deliver the rest of the integerdivision 12 / 10 ( toDelete % someList.size()
) 模运算符将提供其余的整数除法12/10(
toDelete % someList.size()
)
You can use this code snippet in a loop in order to remove multiple elements. 您可以在循环中使用此代码段,以删除多个元素。
If you always want to remove index 2, you could do: 如果您始终要删除索引2,则可以执行以下操作:
l = j - array.size();
Change this line as below: 更改此行,如下所示:
int sum = 0;
sum = l - array.size();
if (sum > 0) {
array.remove(sum);
} else {
sum = array.size() - l;
array.remove(sum);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.