Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.

Similar presentations


Presentation on theme: "Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT."— Presentation transcript:

1 Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT

2 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

3 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.

4 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.

5 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.

6 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)); }

7 Output 10 20 30 40 50

8 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.

9 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);

10 //Displaying array for (int i=0;i<Intarr.size();i++){ System.out.println(Intarr.get(i)); }

11 0 10 20 25 30 40 50

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

13 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()); }

14 The size of ArrayList is: 5

15 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.

16 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);

17 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)); }

18 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:");

19 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)); }

20 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.

21 Vector class Import java.util.Vector;

22 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.

23 By Specifying Size: Vector array_name = new Vector (int size);

24 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);

25 System.out.println("The vector :"); for(int i=0;i<V.size();i++) { System.out.println(V.get(i)); }

26 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.

27 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);

28 System.out.println("The vector :"); for(int i=0;i<V.size();i++) { System.out.println(V.get(i)); }

29 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);

30 Intarr.add(50); //Displaying ArrayList size System.out.println("The size of Vector is: " + Intarr.size()); }


Download ppt "Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT."

Similar presentations


Ads by Google