The document discusses ArrayLists in Java. Key points include:
- ArrayLists allow dynamic resizing as elements are added, with O(1) access time. They support methods for insertion, deletion, and updating elements.
- ArrayLists can be iterated over using a for-each loop or indexed access.
- Common ArrayList methods include add(), get(), contains(), clear(), remove(), and size().
- Iterators can be used to iterate over ArrayLists in a single direction. ListIterators allow bidirectional traversal and modification of elements.
1. Array List:
import java.util.ArrayList;
It allow expandable array dynamically.
An arraylist automatically expand as data is added.
Access to any element of an array list is O(1);
Arraylist has method for inserting ,deleting and updating;
Arraylist can be traversed using for each loop or indexes.
Syntax:
ArrayList arrylist-name= new ArrayList();
e.g. arry list name fruits.
Import java.util.ArrayList;
Public class fruits
{
Public static void main(String arr[])
{
ArrayList fruits=new ArrayList();
Fruits.add(“apple”);
Fruits.add(“orange”);
Fruits.add(“mango”);
Fruits.add(“lichi”);
System.out.println(fruits.size());
For( int i=0;i<fruits.size();i++)
{
System.out.println(fruits.get(i));
}
2. }
Methods in arraylist:
Method description
Adding elements a.add(e) Add e to end of arraylist
a.add(i,e) Insert at index i ,shifting element up
as necessary
Replacing elements a.set(I,e) Set the element at index I to e
Getting element E=a.get(i) Return the objects at index i
searching B=a.contains(e) Return true if array list conatin e
Removing a.clear() Remove all elements from arraylist
a.remove(i) Remove elements at index i
a.removeRange(I,j) Remove elements from position I to j
Import java.util.ArrayList;
Class fruits
{
Public static void main(String []arr)
{
ArrayList<Integer> arrayList= new ArrayList<Integer>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);for(i=0;i<arrayList.size();i++)
{
System.out.println arrayList.get(i).int value());
}
}
3. Java Iterator:
To generate successive elements from a series we can use java iterator. It is an improvement over
enumeration interface.
We can iterate only in one direction.
Iteration can be done only once . if we need to iterate again we should get a new
iterator.
Import java.util.ArrayList;
Import java.util.Iterator;
Class ExampleIterator
{
Public static void main(String arr[])
{
ArrayList animal=new ArrayList();
Animal.add(“Horse”);
Animal.add(“Lion”);
Animal.add(“tiger”);
Iterator ani=animal.iterator();
While(ani.hasNext())
{
String aniob=(String)aniob.next();
System.out.println(aniob);
}
}
}
4. List iterator:
It allow bidirectional traversal of a list and modification of elements.
Import java.util.*;
Class Iteratordemo
{
Public static void main(String arr[])
{
ArrayList<String> a1=new ArrayList<String>();
A1.add(“alpha”);
A1.add(“beta”);
A1.add(“gamma”);
A1.add(“eta”);
//use of iterator to display the content
Iterator<String>itr=a1.iterator(); //obtain iterator
While(itr.hasNext()) //check for next
System.out.println(itr.next()+” “); // obtain next element
System.out.println(); //print blank line
//use of iterator to remove element
Itr=a1.iterator;
While(itr.hasNext())
{
If(itr.next().equals(“gamma”));
Itr.remove(); //use of iterator for remove
}
//again display after deletion
5. Itr.a1.iterator();
While(itr.hasNext())
System.out.println(itr.next()+ “ “);
System.out.println();
//now use of listiterator to add gamma back to list
ListIterator<String> litr =a1.listIterator(); //obtain list iterator
While(litr.hasNext())
{
If(litr.next().equals(“beta”));
Litr.add(“gamma”); //add element using list iterator.
}
//content after addition of gamma
Litr=a1.listiterator();
While(litr.hasnext())
System.out.println(litr.next()+” “);
System.out.println();
//use of listiterator to mdify the object being iterated.
String str;
Litr=a1.listIterator();
While(litr.hasnext())
{
Str=litr.next();
If(str.equals(“alpha”);
Litr.set(“A”); //use of iterator to change element
Else if(str.equals(“beta”)
6. Litr.set(“B”);
}
//use of list iterator to display in backward
While(litr.hasprevious())
{
System.out.println(litr.previous()+” “);// display list in reverse order
}
System.out.println();
}
}
For each Loop:
To make iteration over array convenient we can use for each loop
For(type var:arr)
{
Body of loop;
}
e.g
double []arr={1,2,3,4,5,6,7,8};
int sum=0;
for(double d:arr)
{
Sum +=d;
}
e.g
int arr[]={ 12,13,14,15};
7. for(int i:arr)
{
System.out.println(i);
}
Scanner class:
To facilitate the user input we can use the java library scanner class. Scanner directly
intract with system input buffer for values.
e.g
import java.util.Scanner;
class Scan
{
Public static void main(String arr[])
{
Scanner s=new Scanner(System.in);
Int a=s.nextInt();
Int b=s.nextInt();
Some methods to be use with scanner class.
nextByte(); will accept byte from input
nextShort(); will accept short
nextLong(); will accept long
nextLine(); accept line of string
nextBoolean(); accept boolean
next float(); accept float
8. Array:
The similar data type elements, variable or objects can be stored in the array.
Array can store homogenous data type variable.
Array in C/C++ and Java are totally different.
In java array are objects
e.g
int array[]; // here array is set to null there is nothing in the array
if we want to the actual physical array on memory then we have to allocate the memory
with new
so
int array[];
array[]=new int[12];
after this statement is executed arra will refer to array of 12 integer with no value but by default
there is zero values in the array.
Array can also be initialized when they are declared
Like’
Int array[]={12,13,12,13,12,13,12,13,12,13};
Two dimensional array:
Int twoD[][]=new int[4][5];
Class twoD
{
Public static void main(string arr[])
{
9. Int twoD[][]=new int[4][5];
Int I,j,k=0;
For(i=0;i<4;i++)
For(j=0;j<5;j++)
{
twoD[i][j]=k;
k++;
}
For(i=0;i<4;I++)
{
For(j=0;j<5;j++)
System.out.println(twoD[i][j] +” “);
System.out.println();
}
}
}
e.g find the average ofinteger stored in array
class Avg
{
Public static void main(String arr[])
{
Double num[]={12.2,13.3,14.5,123.5,233.45,23.1};
Double result=0;
Int i=0;
For(i=0;i<5;i++)
10. Result=result+num[i];
System.out.println(“average=”+result/6);
}
}
}
Irregular array:
Int twoD[][]=new int [4][];
For first element memory is allocated for four element. For second dimensions we can allocate
manually like
twoD[0]= new int[1]
twoD[1]= new int[2]
twoD[2]= new int[3]
twoD[3]= new int[4]
so we can change the memory dynamically for column
class twoD
{
Public static void main(String arr[])
{
Int twoD[][]=ne twoD[0]= new int[1]
twoD[0]= new int[1];
twoD[1]= new int[2];
twoD[2]= new int[3];
twoD[3]=new int[4];
int I,j,k=0;
for(i=0;i<4;i++)
12. System.out.println(“minmum and maximum;”+min+” “+max);
}
}
e.g two print the common element in two array
code:
int arr1[]={4,5,6,2,1,3,4};
int arr2[]={12,13,14,15,3,2,4};
for(i=0;i<arr1.length;i++)
for(j=0;j<arr2.length;j++)
{
If(arr1[i]==arr2[j])
{
System.out.println(arr1[i]);
}
}
e.g. program to sort the array
import java.util.Arrays;
class myArray
{
p.s.v.m(String arr[])
{
Int[]myarr={3,2,3,2,3,2,4,5,12,0,-2};
Arrays.sort(myarr);
For(int i=0;i<myarr.length;i++)
{