Home C C++ Java Python Perl PHP SQL JavaScript Linux Online Test Forum

Home » Forum » x-Forum » Question

How List data type work in Java please share one example

I want program on List with better examples

Posted by Asif007 2018-09-12 20:09:13

Hi Asif, 
this is one example

import java.util.*; 

public class Main
{
	public static void main(String[] args) {
	    List<Integer> mylist = new ArrayList();
	    mylist.add(5);
	    mylist.add(15);
	    mylist.add(25);
	    System.out.println(mylist.size());
	}
}

C++ user image admin posted 2018-09-12 20:15:08

Thanks for your post

C++ user image Asif007 posted 2018-09-12 20:18:20

 import java.util.*; 
  
public class ListDemo 
{ 
    public static void main (String[] args) 
    { 
        // Creating a list 
        List<Integer> l1 = new ArrayList<Integer>(); 
        l1.add(0, 1);  // adds 1 at 0 index 
        l1.add(1, 2);  // adds 2 at 1 index 
        System.out.println(l1);  // [1, 2] 
  
        // Creating another list 
        List<Integer> l2 = new ArrayList<Integer>(); 
        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);      
        System.out.println(l1); // [1, 2, 3, 2] 
  
        // Prints element at index 3 
        System.out.println(l1.get(3)); 
  
        // Replace 0th element with 5 
        l1.set(0, 5);    
        System.out.println(l1);  
    } 
} 

C++ user image Marck12 posted 2019-04-17 04:15:28

Your Answer


Ask New Question