Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

List Interface Java

// Java program to Demonstrate List Interface
 
// Importing all utility classes
import java.util.*;
 
// Main class
// ListDemo class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of List interface
         // implemented by the ArrayList class
        List<Integer> l1 = new ArrayList<Integer>();
 
        // Adding elements to object of List interface
        // Custom inputs
 
        l1.add(0, 1);
        l1.add(1, 2);
 
        // Print the elements inside the object
        System.out.println(l1);
 
        // Now creating another object of the List
        // interface implemented ArrayList class
        // Declaring object of integer type
        List<Integer> l2 = new ArrayList<Integer>();
 
        // Again adding elements to object of List interface
        // Custom inputs
        l2.add(1);
        l2.add(2);
        l2.add(3);
 
        // Will add list l2 from 1 index
        l1.addAll(1, l2);
 
        System.out.println(l1);
 
        // Removes element from index 1
        l1.remove(1);
 
        // Printing the updated List 1
        System.out.println(l1);
 
        // Prints element at index 3 in list 1
        // using get() method
        System.out.println(l1.get(3));
 
        // Replace 0th element with 5
        // in List 1
        l1.set(0, 5);
 
        // Again printing the updated List 1
        System.out.println(l1);
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Create a Scanner Object in Java 
Java :: next line java does not take input 
Java :: login.html 
Java :: android click button programmatically 
Java :: what is ioexception in java 
Java :: Java Creating a HashSet 
Java :: switch expression java 
Java :: can we override the overloaded method in java 
Java :: Example of a Do..While Loop 
Java :: java to kotlin converter android studio 
Java :: encapsulation in java 
Java :: import in java 
Java :: map.put in java 
Java :: POST method) in spring rest api 
Java :: variable might not have been initialized error 
Java :: why string is immutable in java 
Java :: spring security controlleradvice 
Java :: javafx 
Java :: java stream Return sums of elements grouped by a remainder of division by the give divisor 
Java :: stream reduce stringbuilder 
Java :: No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing instance of type Main (e.g. x.new A() where x is an instance of Main). 
Java :: show bottom sheet in adapter 
Java :: restore 
Java :: list in list 
Java :: trier un tableau de string java 
Java :: bloomreach clone session 
Java :: inheritance setter and getter in java 
Java :: Iterating an Array Using While Loop 
Java :: do i have to import files from the same package in java 
Java :: validate text field netbeans emails 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =