When the size of an array is unknown at the run time we need to create dynamic array. In java there

are plenty of ways in which we can build array dynamically . In that, i am choosing one of the easy way.

It’s through ‘Arraylist’. Here we will take a tour from ‘How to create an Arraylist’ to ‘How to use

arraylist’.

Syntax for creating an arraylist is

ArrayList arraylist_name = new ArrayList();

Here is a simple code which creates an arraylist named fruits and adds fruit names to it.

import java.util.ArrayList; 
public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList(); //creating a new arraylist
		fruits.add("apple"); // adding fruit names to it
		fruits.add("orange");
		fruits.add("mango");
	}
}

okay, now we have created an arraylist..Now, lets see how many elements we have added to it. Use size()

function to get the size of it.

import java.util.ArrayList;
public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList(); //creating a new arraylist
		fruits.add("apple"); // adding fruit names to it
		fruits.add("orange");
		fruits.add("mango");
		System.out.println("The size of fruit array list is" + fruits.size());
	}
}

now, we know that we have added 3 elements in the array. What are all those elements? Know it by

fetching the ArrayList items.

Here i gonna show retrieval process in 2 different types

1. We can make use of an utility called Iterator which will point to the array list item and we can fetch

using the function next();

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		System.out.println(fruits.size());
		 Iterator itr = fruits.iterator();
		 while(itr.hasNext()) // loop thro elements
			 System.out.println(itr.next()); //print the element itr pointing to
				
	}
}

2. we can also use the for loop to retrieve the items by using the function get()

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		System.out.println(fruits.size());
		 for(int i=0;i<fruits.size();i++) // run loop till size of arraylist
			 System.out.println(fruits.get(i)); // print array using get method		
		
	}

}

now, consider i need get the item in the second position. Then how could i do that? Its so simple. We

know the index. Just put it in the get method;

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		System.out.println(fruits.size());
		System.out.println(fruits.get(1)); // print second element in array		
		
	}

}

Note: The array index starts from 0. Hence for second element in array , the index will be 1

When we use dynamic array, the size of the array can be changed right ? So, consider we need to insert

a new fruit named ‘grape’. But we need to put it in between apple and orange. How could we do that?

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		fruits.add(1,"grape"); // adding element in the index 1 i.e adding as second element 
		System.out.println(fruits.size());
		for(int i=0;i<fruits.size();i++)
			 System.out.println(fruits.get(i));
		
	}

}

when we can add element in between an array, we can also delete the element. Do it be specifying the

index or by the element name itself.

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList fruits = new ArrayList();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		fruits.remove(0); // remove the first element
		fruits.remove("grape"); // remove the element named grape
		System.out.println(fruits.size());
		for(int i=0;i<fruits.size();i++)
			 System.out.println(fruits.get(i));
		
	}

}

Till now we went through the process of insertion and deletion. Here comes the datatype. We can store

different data types in Arraylist. Lets see one by one.

1. Storing boolean type in ArrayList

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {
		
		ArrayList<Boolean> arrayList = new ArrayList<Boolean>();
		arrayList.add(true);
		arrayList.add(false);
		arrayList.add(true);
		arrayList.add(false);
		 for(int i=0;i<fruits.size();i++)
			 System.out.println(arrayList.get(i));
				
	}

}

2. Storing Integer Datatype

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {
		
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		arrayList.add(1);
		arrayList.add(2);
		arrayList.add(3);
		arrayList.add(4);
		 for(int i=0;i<fruits.size();i++)
			 System.out.println(arrayList.get(i).intValue());
				
	}

}

while using Integer datatype we need to be careful while retrieving.. If we are retriving like arraList.get

(i), we wont get the integer number. Hence, to convert it into primitive int we need to add get.intValue()

3. Storing Strings in arraylist

import java.util.ArrayList;
import java.util.Iterator;


public class fruits {

	public static void main(String[] args) {

		ArrayList<String> fruits = new ArrayList<String>();
		fruits.add("apple");
		fruits.add("orange");
		fruits.add("mango");
		System.out.println(fruits.size());
		 for(int i=0;i<fruits.size();i++) 
			 System.out.println(fruits.get(i));
		
	}

}
Advertisements

Comments are closed.