My take...does not require the size precondition check but you may want to still catch that if it represents an error of broader scope than this method.
Given this test code...
// Test code
List<String> myList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
myList.add(String.valueOf(i));
}
the 'zeilen' loop can be implemented as ...
// "before" diagnostics
System.out.println(zeilen);
// The 'zeilen' loop
for (int i = 0, limit = zeilen.size(); i < limit; i++) {
if ((i+1) % 4 > 0) zeilen.remove(i/4);
}
// "after" diagnostics
System.out.println(zeilen);
and produces
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[3, 7, 11, 15, 19]
Works with any length list leaving every '4th' element in list.
A few more test cases :
Given Results in
[] []
[0,1] []
[0,1,2,3] [3]
[0,1,2,3,4] [3]
[0,1,2,3,4,5,6,7] [3,7]
[0,1,2,3,4,5,6,7,8] [3,7]
Answer from user17856705 on Stack OverflowMy take...does not require the size precondition check but you may want to still catch that if it represents an error of broader scope than this method.
Given this test code...
// Test code
List<String> myList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
myList.add(String.valueOf(i));
}
the 'zeilen' loop can be implemented as ...
// "before" diagnostics
System.out.println(zeilen);
// The 'zeilen' loop
for (int i = 0, limit = zeilen.size(); i < limit; i++) {
if ((i+1) % 4 > 0) zeilen.remove(i/4);
}
// "after" diagnostics
System.out.println(zeilen);
and produces
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[3, 7, 11, 15, 19]
Works with any length list leaving every '4th' element in list.
A few more test cases :
Given Results in
[] []
[0,1] []
[0,1,2,3] [3]
[0,1,2,3,4] [3]
[0,1,2,3,4,5,6,7] [3,7]
[0,1,2,3,4,5,6,7,8] [3,7]
As said in the comments, removing an element impacts the indexing. Whenever I need to do something like this, I either use an Iterator, or loop backwards.:
for (int i = zeilen.size() - 4; i >= 0; i -= 4) {
zeilen.remove(i + 2);
zeilen.remove(i + 1);
zeilen.remove(i);
}
Note that I subtract 4 from i each iteration, so I go back a full block of four each time.
Also note that I remove the largest indexed elements first. If I use i, i + 1 and i + 2 inside the loop, I again run into the same issue. I could also have used i 3 times, but this makes it more clear.
list.subList(4, list.size()).clear();
Sublist operations are reflected in the original list, so this clears everything from index 4 inclusive to list.size() exclusive, a.k.a. everything after index 3. Range removal is specifically used as an example in the documentation:
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
list.subList(from, to).clear();
Using sublist() and clear(),
public class Count
{
public static void main(String[] args)
{
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
arrayList.subList(2, arrayList.size()).clear();
System.out.println(arrayList.size());
}
}
One thing you need to keep in mind is that when you use ArrayLists that they are meant to be versatile, moreso than Arrays. You can shorten an array by removing an entire index, add an index to it, and do wonderfulness with ArrayLists.
This is a common problem with people who do not realize, or remember, that when you remove a value, the ArrayList indexes (or whatever the correct plural is) readjust and the ArrayList shortens.
When attempting to remove elements from an ArrayList, you should always start at the end of the ArrayList.
for(int x = arrayList.size() - 1; x > 0; x--)
{
arrayList.remove(x);
}
This should provide you with the function that you are looking for. Take a look at the ArrayList API for other methods that may help you.
Use Iterator.remove() to remove elements while iterating.
for (Iterator<String> iter = myarraylist.iterator(); iter.hasNext(); ) {
String element = iter.next();
if (element meets some criteria) {
iter.remove();
}
}
Or use Google Guava's filter which returns a filtered view and leaves the original list unchanged.
Iterable<String> filtered = Iterables.filter(myarraylist, new Predicate<String>() {
public boolean apply(String element) {
return true of false based on criteria
}
});
You can use List#subList(int, int):
List<Integer> list = ...
list = list.subList(10, list.size()); // creates a new list from the old starting from the 10th element
or, since subList creates a view on which every modification affects the original list, this may be even better:
List<Integer> list = ...
list.subList(0, 10).clear(); // clears the first 10 elements of list
Just use the remove() method for this.
Let's assume you want to remove elements with indices from 20 to 30 of an ArrayList:
ArrayList<String> list = ...
for (int i = 0; i < 10; i++) { // 30 - 20 = 10
list.remove(20);
}
Once the first element at index 20 is removed element 21 moves to index 20. So you have to delete 10 times the element at index 20 to remove the next 10 elements.