How to insert an object in an ArrayList at a specific position

后端 未结 7 1212
挽巷
挽巷 2020-11-29 22:08

Suppose I have an ArrayList of objects of size n. Now I want to insert an another object at specific position, let\'s say at index position k (is greater than 0 and less tha

7条回答
  •  感动是毒
    2020-11-29 22:21

    Here is the simple arraylist example for insertion at specific index

    ArrayList str=new ArrayList();
        str.add(0);
        str.add(1);
        str.add(2);
        str.add(3); 
        //Result = [0, 1, 2, 3]
        str.add(1, 11);
        str.add(2, 12);
        //Result = [0, 11, 12, 1, 2, 3]
    

提交回复
热议问题