Skip to content
Advertisement

How can I use loop here, I want to remove 3 elements from list for every turn

I have a list that has some elements, I need to print the first 3 elements when page num = 1, then next 3 elements when page ==2 likewise

public class OtherTemporaryTrials {

    public static void main(String[] args) {
        
        int numOfPages = 6;
        
        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(13);
        al.add(77);
        al.add(53);
        al.add(63);
        al.add(173);
        al.add(134);
        al.add(1366);
        al.add(13446);
        al.add(1333);
        al.add(1323);
        al.add(163);
        al.add(1335);
        al.add(13228);
        al.add(1573);
        al.add(13355);
        al.add(12343);
        al.add(12353);
        
        
        
        
        for(int i=0;i<numOfPages;i++) {
            
            setdata(al,numOfPages);
            
        }
    }

    private static void setdata(ArrayList<Integer> al, int numOfPages) {
        
        
        for(int i=0;i<3;i++) {

// Here for every next page we should get the next 3 elements from List
            System.out.println(al.get(i));
        }
        
    }

}

Advertisement

Answer

To access the nth page with a page size of k, you need to get the elements in the range n * k to (n + 1) * k. This is easily done using the subList API:

public class Main {
    public static List<Integer> getPage(List<Integer> list, int pageSize, int pageNo) {
        // get the from & to range bounds, respecting the list length to prevent IndexOutOfBound exceptions
        int from = pageNo * pageSize;
        int to = Math.min(list.size(), (pageNo + 1) * pageSize);
        
        // return the expected slice of the list
        return list.subList(from, to);
    }

    public static void clearPage(List<Integer> list, int pageSize, int pageNo) {
        // get the from & to range bounds, respecting the list length to prevent IndexOutOfBound exceptions
        int from = pageNo * pageSize;
        int to = Math.min(list.size(), (pageNo + 1) * pageSize);
        
        // clear the expected slice from the list
        // this is allowed as long as the list is not "read-only"
        list.subList(from, to).clear(); 
    }

    public static void main(String[] args) {
        // need to use an array list because Arrays.asList return an immutable list (read-only)
        var list = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
        int pageSize = 3;

        // print pages
        System.out.println(getPage(list, pageSize, 0));
        System.out.println(getPage(list, pageSize, 1));
        System.out.println(getPage(list, pageSize, 2));
        System.out.println(getPage(list, pageSize, 3));

        System.out.println(list);

        // delete elements at position 0 to 2
        clearPage(list, pageSize, 0);
        System.out.println(list);

        // delete elements at position 3 to 5
        clearPage(list, pageSize, 1);
        System.out.println(list);
    }
}

Because this works with zero-based indices, if you want 1 to represent the first page, you need to substract 1 from the page number before using it to calculate the range bounds of subList.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement