Get arraylist 10 items from some position

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.


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

Java ArrayList.get() Method with example: The get() method is used to get the element of a specified position within the list.


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

Java.util.ArrayList.get() Method - The java.util.ArrayList.get(int index) method returns the element at the specified position in this list.


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.

Find the position of an item in the ArrayList: IndexOf: 1. Item to find. 2. Position to start searching from. Dim index As Long ' Search from 0 position index = fruit.IndexOf("Pear", 0) Get number of items: Count: None: totalElements = list.Count: Insert Item: Insert: 1. Index - position to insert at. 2 Value - object or value to insert. list


So to access the first position of the Array List, we use the [0] index position. And so on and so forth. If the above code is entered properly and the program is run the following output will be displayed. Output: From the output, you can see that all of the elements from the array list are sent to the console.


Examples. The following code example creates an ArrayList and adds several items. The example demonstrates accessing elements with the Item[] property (the indexer in C#), and changing an element by assigning a new value to the Item[] property for a specified index.


ArrayList.Item[Int32] Property is used to get or set the element at the specified index in ArrayList. Syntax: public virtual object this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: It returns the element of Object type at the specified index.