Remove elements from a List at a specific index

Ayoub

I am trying to program a method that deletes the first, second and third element of every group of 4 elements. It seems not working at all. Could anyone please help?

public static void reduziereKommentare(List<String> zeilen) {
        if (!zeilen.isEmpty()) {
            if (zeilen.size() % 4 != 0) {
                throw new RuntimeException("Illegal size " + zeilen.size() + " of list, must be divisible by 4.");
            }
            for (int i = 1; i <= zeilen.size() % 4; i++) {
                zeilen.remove(i);
                zeilen.remove(i + 1);
                zeilen.remove(i + 2);
            }
        }
        System.out.println(zeilen);
    }
Gardener

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove multiple elements from a list of index with Python

Remove specific elements from list Python

remove item from a list by specific index

Remove duplicates from a list and remove elements at same index in another list

javascript add and remove multiple value from a list in and from specific index

Remove all elements from a List after a particular index

How to make a function to remove elements by index from a list

Prolog remove elements which have the same value as the index from the list

Remove few elements from a Map of String to List for a specific key

Remove elements from list which are just before some specific element

Use remove() function to delete elements containg specific substring from list

Remove elements from list if appears more than a specific value in python

Remove an entry from a List<int[]> based on the value of of a specific index

Remove a specific index from a list while iterating in Python

How to remove specific index from a list of datagridview rows

How to remove list of list at specific index

inserting all the elements from a list into specific index of sublist from a second list - python 2

Can't remove specific elements from array by index number from dynamic div element in react?

Remove elements from a Linked List

Remove duplicated elements from list

Remove multiple elements from a List

Remove elements from python list

Remove elements from a list C

Remove null elements from list

Remove older elements from a list

Compare and remove elements from list

Remove elements from a list in karate?

Remove duplicates from list elements

Remove elements from a list by condition