Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Add an element at a specified position in an ArrayList

import java.util.ArrayList;

class Main {
  public static void main(String[] args){

    // create ArrayList
    ArrayList<String> Websites = new ArrayList<>();

    // add() method without the index parameter
    Websites.add("Softhunt.net");
    Websites.add("Pythonprogramming.com");
    Websites.add("Tutorial.io");
    
    // add JavaScript.com at index 1
    Websites.add(0, "JavaScript.com");

    // add C++.net at index 3
    Websites.add(4, "C++.net");
    System.out.println("ArrayList: " + Websites);
  }
}
Source by softhunt.net #
 
PREVIOUS NEXT
Tagged: #Add #element #position #ArrayList
ADD COMMENT
Topic
Name
1+7 =