java insert element at position

Solutions on MaxInterview for java insert element at position by the best coders in the world

showing results for - "java insert element at position"
Nico
25 Jun 2020
1package com.tutorialspoint;
2
3import java.util.ArrayList;
4
5public class ArrayListDemo {
6   public static void main(String[] args) {
7
8      // create an empty array list with an initial capacity
9      ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
10
11      // use add() method to add elements in the list
12      arrlist.add(15);
13      arrlist.add(22);
14      arrlist.add(30);
15      arrlist.add(40);
16
17      // adding element 25 at third position
18      arrlist.add(2,25);
19        
20      // let us print all the elements available in list
21      for (Integer number : arrlist) {
22         System.out.println("Number = " + number);
23      }  
24   }
25}