简体   繁体   English

java.lang.IllegalArgumentException: fromIndex(x) > toIndex(y)

[英]java.lang.IllegalArgumentException: fromIndex(x) > toIndex(y)

I convert list to page for display table of candidates with pagination, but I am getting error java.lang.IllegalArgumentException: fromIndex(5) > toIndex(1), How to solve this issue please?我将列表转换为页面以显示带有分页的候选人表,但我收到错误 java.lang.IllegalArgumentException: fromIndex(5) > toIndex(1),请问如何解决这个问题?


 public Page<CandidatesDetailsDto> filterCandidates(Integer page, Integer pageSize) {

        page = (page == null || page < 1) ? 0 : page - 1;
        pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;

        PageRequest pageRequest = new PageRequest(page, pageSize);

           List<CandidatesEntity> candidatesEntityList2 = candidatesService.findAll(pageRequest);

            int start = (int) pageRequest.getOffset();
            int end = (start + pageRequest.getPageSize()) > candidatesEntityList2.size() ? candidatesEntityList2.size() : (start + pageRequest.getPageSize());

            candidatesEntity = new PageImpl<CandidatesEntity>(candidatesEntityList2.subList(start, end), pageRequest, candidatesEntityList2.size());
        }

        return candidatesEntity.map(source -> candidatesDetailsConverter.convertTo(source));
    } 



This exception happens because when computing start of your slice you don't take into consideration how many objects are there in full list of candidates.发生此异常是因为在计算切片的开始时,您没有考虑完整的候选列表中有多少对象。

If I request from your method a page 1 of size 5, but you only have 2 candidates, look what happens:如果我从您的方法中请求大小为 5 的第 1 页,但您只有 2 个候选人,请看看会发生什么:

int page = 1;
int pageOffset = 5; // page * pageSize
List<Candidate> all = Arrays.asList(customer1);

int start = 5; // = pageOffset
int end = 1;   // Math.min(start, all.size()); because min(5, 1) == 1.

Page<Candidates> page = new PageImpl(all.subList(start, end)); // this is what throws

You have to especially consider the case when someone requested from you the page that does not exist.当有人向您请求不存在的页面时,您必须特别考虑这种情况。 In that case, you have to either return an empty page, or a null , or something else, but important part is it has to be processed on a different code path.在这种情况下,您必须返回一个空页面或null或其他内容,但重要的是它必须在不同的代码路径上处理。

So, here's some code that should almost work: you have to implement the nonExistentPage method yourself.所以,这里有一些几乎可以工作的代码:你必须自己实现nonExistentPage方法。

public Page<CandidatesDetailsDto> filterCandidates(Integer page, Integer pageSize) {

    page = (page == null || page < 1) ? 0 : page - 1;
    pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;

    PageRequest pageRequest = new PageRequest(page, pageSize);

    List<CandidatesEntity> candidatesEntityList2 = candidatesService.findAll(pageRequest);

    int start = (int) pageRequest.getOffset();

    if (start <= candidatesEntityList2.size()) {
      return nonExistentPage(candidatesEntityList2.size()); // important part here
    }
    int end = Math.min(start + pageRequest.getPageSize(), candidatesEntityList2.size());

    candidatesEntity = new PageImpl<CandidatesEntity>(candidatesEntityList2.subList(start, end), pageRequest, candidatesEntityList2.size());

    return candidatesEntity.map(source -> candidatesDetailsConverter.convertTo(source));
}

/*
 * Implement this yourself: you must convey to the user that the page 
 * he requested from you does not exist.
 */
private abstract Page<CandidatesDetailsDto> nonExistentPage(int fullListSize);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM