0

such a problem, there are 100 positions in the list, I only get the position, and from this position I want to receive the following 10 items.For example, I get 30 and I want to return positions 30-40

I tried sublist(30,30+10) but when it comes to the end, an error.

ArrayList<Model> numList = new ArrayList<>();

in numList i have 100 items;

ArrayList<Model> numList2 = new ArrayList<>();

in numlist2 i want to be from 30 to 40, but I have only a starting position of 30 and the size that I need is 10.

6
  • What is the problem? Oct 21, 2019 at 10:09
  • @Ioannis Barakos i can`t do it)
    – Azarnoy
    Oct 21, 2019 at 10:11
  • what should happen when list has 30 items and you got the number 25, so starting from 25 +10 = 35, now you don't have the extra 5 numbers after 30 so from where you will get that? in this case it should return 5 numbers of what? or it should start over again from 1 ?
    – Dev
    Oct 21, 2019 at 10:11
  • What is the problem with having 100 items in the list and create a new sublist(30,40). This should work Oct 21, 2019 at 10:12
  • params.startPosition and params.loadSize, there i get position and size. For now i have list with 100 items, and position i get 10,20,30 etc. when i get 10 i need 10-20, when 20 - 20-30 etc
    – Azarnoy
    Oct 21, 2019 at 10:14

3 Answers 3

3

The javadoc always is informative.

List<Model> numList2 = numList.subList(30, 40); // 40 exclusive

numList2 is backed by numList in both directions:

 numList2.set(0, ...); // numList.get(30) changed
 numList.set(31, ...); // num2List.get(1) changed

Independent list:

List<Model> numList2 = new ArrayList(numList.subList(30, 40));

List<Model> get10(List<Model> numList, int i) {
    if (i >= numList.size()) {
        return new ArrayList<>();
    }
    int i2 = Math.min(i + 10, numList.size());
    return new ArrayList(numList.subList(i, i2));
}
1
  • i dont know second number, i just have 30 and all, and i know that i need 10 items
    – Azarnoy
    Oct 21, 2019 at 10:16
-1

Here is some approach you can try.. Just Replace your class with Integer from below code.

ArrayList<Integer> numList = new ArrayList<>(); //Replace Integer with your class

IntStream.range(1, 101).forEach(v -> numList.add(v)); //Filled out data

ArrayList<Integer> numList2 = numList.subList(30, 30 + 10); //Print 30 to 40
numList2 .forEach(val -> {
    System.out.println("data: " + val);
});;
-1

public List subList(int fromIndex,int toIndex)

You can use the above method.

ArrayList numList2 = new ArrayList(numList.subList(30, 40));

    if(toIndex<=numList .size()) {
        retutn numListsubList(fromIndex, toIndex);
    }else {
        return new ArrayList<Model>();
    }

where fromIndex and toIndex are variables and can be passed as per your requirement.

4
  • i dont know second number, i just have 30 and all, and i know that i need 10 items
    – Azarnoy
    Oct 21, 2019 at 10:20
  • ArrayList numList2 = new ArrayList(numList.subList(30, 30+10)); or ArrayList numList2 = new ArrayList(numList.subList(30, 30+20));// in case required 20 lelements. fromIndex and toIndex in subList(int fromIndex,int toIndex) method are variables and you can pass these parameters as per your requirement. Oct 21, 2019 at 10:29
  • when i reach to end, an error will be, couse list have 100 items, i try to find 100+10
    – Azarnoy
    Oct 21, 2019 at 10:33
  • In that case you must check for the list size. i.e if toIndex>numList.size() you should not directly add 10 into toIndex. You should perform this operation only if toIndex<=numList.size() Oct 21, 2019 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.