有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我如何在这里使用循环,我想每转一圈就从列表中删除3个元素

我有一个包含一些元素的列表,当page num=1时,我需要打印前3个元素, 当page==2时,接下来的3个元素也是

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));
        }
        
    }

}


共 (1) 个答案

  1. # 1 楼答案

    要访问页面大小为knth页面,需要获取范围为n * k(n + 1) * k的元素。这可以使用subListAPI轻松完成:

    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);
        }
    }
    

    因为这适用于基于零的索引,如果希望1表示第一页,则需要从页码中减去1,然后再使用它计算子列表的范围边界