Monday, December 9, 2019

ArrayListinJava
ARRAY LIST IN JAVA :

ArrayList is basically dynamic array in JAVA. It is a part of collection framework.

ArrayList is using always list interface.

Eg:-
ArrayList<objType> arraylist=new ArrayList<objType>();

Basic codes on ArrayList:

Ex.- 1.
import java.util.*;
public class MyClass {
    public static void main(String args[]) {
     ArrayList<Integer> id=new ArrayList<Integer>();
     ArrayList<String> name=new ArrayList<String>();
     ArrayList<Integer> age=new ArrayList<Integer>();
     ArrayList<Integer> fee=new ArrayList<Integer>();
     ArrayList<String> address=new ArrayList<String>();
     Scanner scan=new Scanner(System.in);
   int n=scan.nextInt();
   for(int j=0;j<n;j++)
    {
        id.add(scan.nextInt());
        name.add(scan.next().trim());
        age.add(scan.nextInt());
        fee.add(scan.nextInt());
        address.add(scan.next().trim());
         System.out.println("id = "+id.get(j)+" name = "+name.get(j)+" age = "+age.get(j)+" fee = "+fee.get(j)+" address = "+address.get(j));
    }
    
    
    }
}


Ex.- 2.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
     ArrayList<Integer> li=new ArrayList<Integer>();
     li.add(10);
     li.add(0,12);
   Iterator it= li.iterator();
   while(it.hasNext())
   {
       System.out.println(it.next());
   }
    }
}

Ex. - 3.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
     ArrayList<Integer> li=new ArrayList<Integer>();
     li.add(10);
     li.add(0,12);
   
    for(int i=0;i<li.size();i++)
    {
         System.out.println(li.get(i)); 
    }
    }

No comments:

Post a Comment

India bans PUBG Mobile and 117 other Chinese apps: Here’s the full list

Best Guruji   India bans PUBG Mobile and 117 other Chinese apps: Here’s the full list For More information subscribe our channel : www.youtu...