473,398 Members | 2,335 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,398 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 4465

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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.