473,287 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,287 developers and data experts.

How to implement ArrayList data structure in Java

package com.ds.algorithms.arraylist;

/**
* @Author pankaj
* @create 4/10/21 6:37 PM
Why to use ArrayList:
is a re-sizable array, also called a dynamic array(Growable array/ resizable Array). It grows its size to accommodate new elements and
shrinks the size when the elements are removed.
---> ArrayList internally uses an array to store the elements. Just like arrays, It allows you to
retrieve the elements by their index
ArrayList is an implementation class for List interface.
Adv of List:
1. duplicates are allowed.
2. Insertion Order is preserved. the way you are going to insert the elements , in the same way you element will be displayed
3. default capacity of arrayList is 10
4. Formula to grow ArrayList= CC*3/2+1 till java 1.6
5. Once new ArrayList got created the older arraylist will be eligible fo GC
6. null insertion is possible in AL
7. homogeneous elements are allowed
NOTE: INTER DS of ArrayList is Dynamic array/Growable array/ resizable Array
-------------When to use ArrayList ----------------------
1. ArrayList is bad Choice for inserting/ deleting element in the middle of AL because it will take more time to shift element
Noe: If you have Frequent insertion/ deletion operation in middle of List best choice to use LL over AL
2.. If your frequent operation is retrieval operation ArrayList is Best Choice.
3. ArrayList and Vector class implements RandomAccess interface, helps you to retrieve element in constant time O(1) irrespective of index
Note: RandomInterface is marker interface(don't have any method)

*/
public class ArrayListCode {

private static final int INITIAL_CAPACITY=10;
private Object [] objectArray;
private int index;
private int size;
ArrayListCode()
{
this.objectArray=new Object[INITIAL_CAPACITY];
this.size=INITIAL_CAPACITY;
}

public void add(Object o)
{
System.out.println("index: "+this.index+" size: "+this.size+" Array size: "+this.objectArray.length);
//check whether array is full
if(this.index== this.size-1)
{
// Object array is full, in this case we need to increase Object Array
increaseObjectArray();
}
// array is not full
objectArray[index]=o;
this.index++;
}
private void increaseObjectArray()
{
this.size=this.size+INITIAL_CAPACITY;
//create new Object array and copy from older Array
Object[] newObjectArray=new Object[this.size];
for (int i=0;i<objectArray.length;i++)
{
newObjectArray[i]=objectArray[i];
}
this.objectArray=newObjectArray;
System.out.println(" ####### index ###### : "+this.index+" size : "+this.size+" array size : "+this.objectArray.length);
}

// code to get()
public Object get(int i) throws Exception
{
// data index is not present in array
if(i>this.index -1)
{
throw new Exception("ArrayIndexOutOfBoundsException");
} //-ve index is passed
else if(i<0)
{
throw new Exception("negative value !!!");
}
return this.objectArray[i];
}
// method to remove or delete element
public void remove(int i) throws Exception {
if(i>this.index-1)
{
throw new Exception("ArrayIndexOutOfBoundsException !!!");
} else if(i<0)
{
throw new Exception("Negative Index !!!");
}
// code to delete object from array
System.out.println("Object is deleted "+this.objectArray[i]);
for (int j=0;j<objectArray.length-1;j++)
{
objectArray[i]=objectArray[i+1];
}// decrease index by one after deletion of object
this.index--;
}

public static void main(String[] args) throws Exception {
ArrayListCode myArrayList=new ArrayListCode();
myArrayList.add(0);
myArrayList.add(1);
myArrayList.add("pankaj");
myArrayList.add("Ravi Kumar");
myArrayList.add(2);
myArrayList.add(3);
myArrayList.add(4);
myArrayList.add(5);
myArrayList.add(6);
myArrayList.add(7);
myArrayList.add(8);
myArrayList.add(9);
myArrayList.add(10);
System.out.println("Print address ::::::::: "+myArrayList);
System.out.println("index 9th element "+myArrayList.get(9));
//myArrayList.remove(10);
System.out.println("index 11th element "+myArrayList.get(11));
System.out.println("index 12th element "+myArrayList.get(12));
myArrayList.remove(2);
System.out.println(myArrayList.get(2));
/* We can handle Error by using try catch or,. directly throw Exception from main method
try {
System.out.println(myArrayList.get(9));
}catch (Exception e){ }*/
}
}
Apr 12 '21 #1
0 4452

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Saravanan Rathinavelu | last post by:
Let's say there is an ArrayList(s) A and B. A has 1 column and 5 rows. B has 10 columns and 10 rows, where column1 on both being the same with additional rows in B. Using ArrayList A need to...
1
by: Bill Mill | last post by:
Hello all, What data structure would you use to implement something analogous to the iTunes search? I imagine that it must be a tree of some sort, but I can't figure out an easy structure for...
6
by: Pavel Maly | last post by:
Hi, how do I access values in an ArrayList which is a part of another ArrayList? I know I can define a class and then it is quite simple, but this is just an auxiliary application to compute some...
4
by: sakcee | last post by:
Hi I hope that I can learn from your experience . I want to know if you have seen a perticular problem and used a perticular data structure , and why? I usually end up using just arrays,...
16
by: ravi | last post by:
I want to implement a dictionary data structure with the features features * autocorrect * autocomplete * spellcheck can any body tell me that which data structure will be best for its...
1
by: jainyi | last post by:
I want to implement pyramidal structure for storing data having 4^i cells at each level where i is level. In each cell ,i will store number of users,their ids and density so that i can compress them...
2
by: sharan | last post by:
Hello Friends I have a problem in Data Structure (In C) i want to emplement a General tree having three child nodes of each parent node ...please send me any simple Example( code) by which i can...
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
3
by: eisman28 | last post by:
Hi, I need to implement a suitable data structure (in c++) for the following problem. Each state has x,y,z coordinates and a value function (x^2+y^2+z^2) Starting with the first state (x1,y1,z1)...
2
by: dseals22 | last post by:
How to implement the perfect hashed data structure using the four basic operations (insert, fetch, delete, and update)? Will I have to use a hashtable to do this? This is my starting pseudocode...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.