java powered by: arvind department of computer science and engineering radha govind group of...

Post on 02-Apr-2015

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JavaPOWERED BY:

ARVINDDEPARTMENT OF COMPUTER SCIENCE AND ENGINEERINGRADHA GOVIND GROUP OF INSTITUTIONS,MEERUT

ArrayList• A java ArrayList is a dynamic array, an array that can grow after it is created.

• Standard arrays are not dynamic i.e. once a standard array is created it cannot grow. These are of fixed size and you cannot add or remove elements after these are created whereas arraylist java allows that.

• ArrayList in java gives fast iteration

• Also gives fast random access

• It is not synchronized

• Java ArrayList provides more powerful insertion than standard arrays

• Search mechanism in ArrayList is better than standard arrays

• Arraylist is comparable to vector class, in that its dynamic array as well as to standard arrays

• ArrayList manipulation is slower as lot of shifting occurs

How to use ArrayList• Import java.util.ArrayList;

• Declaring an arraylist in java

• After importing java ArrayList class it is time to declare an object of ArrayList class. The default way is:

• ArrayList array_name = new ArrayList();

• An array “array_name” of ten size is created. The ArrayList default constructor creates an array of ten size by default.

• You may also use other constructors of ArrayList to declare an array as follows :

• By Specifying Size:

ArraList array_name = new ArrayList (size);

An array with given number in integer will be created.

Java ArrayList example

• The example below shows creating a java ArrayList object. It adds five elements and then for loop is used to display ArrayList elements.

import java.util.*; 

public class ArrayList_example {

        public static void main(String []args) {

               ArrayList Intarr = new ArrayList(5); //Declaring ArrayList

               Intarr.add(10);

               Intarr.add(20);

               Intarr.add(30);

               Intarr.add(40);

               Intarr.add(50);

               //Displaying array

               for (int i=0;i<Intarr.size();i++){

                        System.out.println(Intarr.get(i));

                     }

        }     

 }

Output• 10

• 20

• 30

• 40

• 50

Java ArrayList add Example• The example below shows creating an ArrayList object.

It adds elements to specific index position by using java ArrayList add method.

• import java.util.*; 

•  public class ArrayList_example {

•         public static void main(String []args) {

•                ArrayList Intarr = new ArrayList(5); //Declaring ArrayList

•                Intarr.add(10);

•                Intarr.add(20);

•                Intarr.add(30);

•                Intarr.add(40);

•                Intarr.add(50);

•                Intarr.add(0, 0); //Adding array elements at specific index position

•                Intarr.add(3, 25);

•               

//Displaying array

               for (int i=0;i<Intarr.size();i++){

                       System.out.println(Intarr.get(i));

                   }

        }     

 }

• 0

• 10

• 20

• 25

• 30

• 40

• 50

Java ArrayList size method example

• The example below shows how to use java ArrayList size method.

• import java.util.*; •  public class ArrayList_example {•         public static void main(String []args) {•                ArrayList Intarr = new ArrayList(5); //Declaring

ArrayList•                Intarr.add(10);•                Intarr.add(20);•                Intarr.add(30);•                Intarr.add(40);•                Intarr.add(50);•                //Displaying ArrayList size•                System.out.println("The size of ArrayList is: "

+ Intarr.size());•        }     •  }

• The size of ArrayList is: 5

Use of Contains Method• The example below shows how to use ArrayList contains

method. The ArrayList java contains method returns Boolean. The returned result will be true if given term is found.

package myclasses;

import java.util.ArrayList;

public class Arraylist_one {

public static void main(String[] args) {

ArrayList A=new ArrayList(4);

A.add(10);

A.add(20);

A.add(30);

A.add(40);

for(int i=0;i<A.size();i++)

{

System.out.println(A.get(i));

}

System.out.println("The use of contains:");

System.out.println(A.contains(10));

System.out.println(A.contains(100));

}

}

Remove method• import java.util.ArrayList;• public class Arraylist_one {• public static void main(String[] args) {• ArrayList A=new ArrayList(4);• A.add(10);• A.add(20);• A.add(30);• A.add(40);• for(int i=0;i<A.size();i++)• {• System.out.println(A.get(i));• }• System.out.println("After removal:");

• A.remove(0);

• for(int i=0;i<A.size();i++)

• {

• System.out.println(A.get(i));

• }

• System.out.println("The use of contains:");

• System.out.println(A.contains(10));

• System.out.println(A.contains(100));

• }

• }

Java vector

• The vector is a class in java that implements dynamic arrays. The standard arrays are not dynamic. That means once created you cannot add or remove elements. Whereas java vector allows to grow or shrink arrays at run time.

A few main points about java vector class:

• Vectors in java implements dynamic arrays

• Vector java class is like ArrayList in that it allows dynamic arrays

• Vector in java is different to ArrayList in that vector is synchronized.

Vector class

• Import java.util.Vector;

Declaring a Java Vector

Vector array_name = new Vector();

An array “array_name” of ten size is created. The Vector default constructor creates an array of ten size by default.

• By Specifying Size:

Vector array_name = new Vector (int size);

• package myclasses;

• import java.util.Vector;

• public class Vector_example {

• public static void main(String[] args) {

• Vector V=new Vector(5);

• V.add(10);

• V.add(20);

• V.add(30);

• V.add(40);

• V.add(50);

• System.out.println("The vector :");

• for(int i=0;i<V.size();i++)

• {

• System.out.println(V.get(i));

• }

• }

• }

Java Vector add Example• The example below shows creating an Vector object. It

adds elements to specific index position by using java vector add method.

• package myclasses;

• import java.util.Vector;

• public class Vector_example {

• public static void main(String[] args) {

• Vector V=new Vector(5);

• V.add(10);

• V.add(20);

• V.add(30);

• V.add(40);

• V.add(50);

• V.add(1,100);

• System.out.println("The vector :");

• for(int i=0;i<V.size();i++)

• {

• System.out.println(V.get(i));

• }

• }

• }

Size of Vector• import java.util.*; • public class vector_example {•         public static void main(String []args) {•                Vector Intarr = new Vector(5);

//Declaring ArrayList•                Intarr.add(10);•                Intarr.add(20);•                Intarr.add(30);•                Intarr.add(40);

•   Intarr.add(50);

•      //Displaying ArrayList size

•       System.out.println("The size of Vector is: " + Intarr.size());

•         }     

• }

top related