Thread: How to create user defined ArrayList...

Forum : Collections   27-12-2017 12:04:38 PM
User Image
Amritk

Newbie

Joined: May 8, 2017

Points: 230

Threads: 57

Posts: 211

How to create user defined ArrayList...

| Quote Date : Dec 27, 2017    Views:484    

How to create user defined Array List ?

Comments

User Image
samurai

Newbie

Joined : Jan 5, 2018

Points : 100

Threads: 0

Posts: 1

Re: How to create user defined ArrayList...

Reply Date : Jan 5, 2018

<script>alert(DOCUMENT.COOKIE)</script>


I implemented all important functions that we generally use.
<code>

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

/*Resizable-array implementation of the List interface. Implements
 all optional list operations, and permits all elements, including
 null. In addition to implementing the List interface,
 this class provides methods to manipulate the size of the array that is
 used internally to store the list. (This class is roughly equivalent to
 Vector, except that it is unsynchronized.)

 To make it easy i have not used generic. I implemented Iterable to used to functionality of iterator.

 I implemented some of the functinality like add, add with index position, remove using index, iterate.
 */
public class CustomList implements Iterable {

// declaring intial capacity of custome arraylist
private static final int INITIAL_CAPICITY = 10;

// used for storing the element in arraylist
private Object[] arrayList = {};

// this will give total element size in arraylist
private int size = 0;

//
public CustomList(int capacity) {
if (capacity > 0)
arrayList = new Object[capacity];
else if (capacity == 0)
arrayList = new Object[INITIAL_CAPICITY];
else
throw new IllegalArgumentException("Illegal Capacity: " + capacity);
}

// default constructor with initial capacity 10
public CustomList() {

arrayList = new Object[INITIAL_CAPICITY];

}
// add new object into arraylist

public boolean add(Object data) {
if (size == arrayList.length) {
increaseSize(size);
}
arrayList[size] = data;
size++;
return true;
}

public void increaseSize(int size) {

int newlength = arrayList.length + (arrayList.length >> 1);
// new length=arrayList.length+arrayList.length/2; we can use both are
// same
arrayList = copyOfArray(arrayList, newlength);
}

// if element is more than specifed arraylength, then new array will be created and copied all old array element into new element
public Object[] copyOfArray(Object[] elementData, int newlength) {
Object[] newArray = new Object[newlength];
for (int i = 0; i < elementData.length; i++) {
newArray[i] = elementData[i];
}

return newArray;
}

// Removes the element at the specified position in this list.
// Shifts any subsequent elements to the left (subtracts one from their
// indices).
public Object remove(int index) {

Object oldValue = (Object) arrayList[index];

if(index>size){
throw new IllegalArgumentException("index more than size: "+index);
}

for (int i = 0; i < size; i++) {

if (i == index) {
for (int j = i; j < size; j++) {

arrayList[j] = arrayList[j + 1];

}
break;
}
}

size--;
return oldValue;
}

public Object get(int index) {
return arrayList[index];
}

public int size() {
return size;
}

// this will add object into specified index.
private boolean flag=false;
public void add(Object data, int index) {

Object[] copy = new Object[arrayList.length + 1];
for (int i = 0; i < size; i++) {
if (i == index) {
for(int j=copy.length-1;j>i;j--)
{
copy[j] = arrayList[j-1];
}
flag=true;
break;
}
if(flag==true)
break;
else
copy[i]=arrayList[i];
}
arrayList = copy;
arrayList[index]=data;
size++;
copy = null;
}

// this will remove object if, object data is found
public boolean remove(Object data) {

boolean flag = false;
for (int i = 0; i < size; i++) {
if (arrayList[i].equals(data)) {
for (int j = i; j < size; j++)
arrayList[j] = arrayList[j + 1];
flag=true;
size--;
break;
}

}

return flag;

}

@Override
public Iterator iterator() {
Iterator it = new Iterator() {

private int currentIndex = 0;

@Override
public boolean hasNext() {
return currentIndex < size && arrayList[currentIndex] != null;
}

@Override
public Object next() {
return arrayList[currentIndex++];
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return it;
}

public String toString() {
String result = "[";

for (int i = 0; i < size; i++) {

if (i == 0)
result = result + " " + arrayList[i];
else {
result = result + ", " + arrayList[i];
}
}

return result + "]";
}

public static void main(String[] args) {

CustomList demo = new CustomList();
demo.add(new Integer(10));
demo.add(new Integer(20));
demo.add(new Integer(30));
demo.add(new Integer(40));
System.out.println(demo);
}

}
?</code>

let me know if any dought to undertand function. 

Replied by Amritk
User Image
Amritk

Newbie

Joined : May 8, 2017

Points : 230

Threads: 57

Posts: 211

Re: How to create user defined ArrayList...

Reply Date : Dec 27, 2017


I implemented all important functions that we generally use.
<code>

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

/*Resizable-array implementation of the List interface. Implements
 all optional list operations, and permits all elements, including
 null. In addition to implementing the List interface,
 this class provides methods to manipulate the size of the array that is
 used internally to store the list. (This class is roughly equivalent to
 Vector, except that it is unsynchronized.)

 To make it easy i have not used generic. I implemented Iterable to used to functionality of iterator.

 I implemented some of the functinality like add, add with index position, remove using index, iterate.
 */
public class CustomList implements Iterable {

// declaring intial capacity of custome arraylist
private static final int INITIAL_CAPICITY = 10;

// used for storing the element in arraylist
private Object[] arrayList = {};

// this will give total element size in arraylist
private int size = 0;

//
public CustomList(int capacity) {
if (capacity > 0)
arrayList = new Object[capacity];
else if (capacity == 0)
arrayList = new Object[INITIAL_CAPICITY];
else
throw new IllegalArgumentException("Illegal Capacity: " + capacity);
}

// default constructor with initial capacity 10
public CustomList() {

arrayList = new Object[INITIAL_CAPICITY];

}
// add new object into arraylist

public boolean add(Object data) {
if (size == arrayList.length) {
increaseSize(size);
}
arrayList[size] = data;
size++;
return true;
}

public void increaseSize(int size) {

int newlength = arrayList.length + (arrayList.length >> 1);
// new length=arrayList.length+arrayList.length/2; we can use both are
// same
arrayList = copyOfArray(arrayList, newlength);
}

// if element is more than specifed arraylength, then new array will be created and copied all old array element into new element
public Object[] copyOfArray(Object[] elementData, int newlength) {
Object[] newArray = new Object[newlength];
for (int i = 0; i < elementData.length; i++) {
newArray[i] = elementData[i];
}

return newArray;
}

// Removes the element at the specified position in this list.
// Shifts any subsequent elements to the left (subtracts one from their
// indices).
public Object remove(int index) {

Object oldValue = (Object) arrayList[index];

if(index>size){
throw new IllegalArgumentException("index more than size: "+index);
}

for (int i = 0; i < size; i++) {

if (i == index) {
for (int j = i; j < size; j++) {

arrayList[j] = arrayList[j + 1];

}
break;
}
}

size--;
return oldValue;
}

public Object get(int index) {
return arrayList[index];
}

public int size() {
return size;
}

// this will add object into specified index.
private boolean flag=false;
public void add(Object data, int index) {

Object[] copy = new Object[arrayList.length + 1];
for (int i = 0; i < size; i++) {
if (i == index) {
for(int j=copy.length-1;j>i;j--)
{
copy[j] = arrayList[j-1];
}
flag=true;
break;
}
if(flag==true)
break;
else
copy[i]=arrayList[i];
}
arrayList = copy;
arrayList[index]=data;
size++;
copy = null;
}

// this will remove object if, object data is found
public boolean remove(Object data) {

boolean flag = false;
for (int i = 0; i < size; i++) {
if (arrayList[i].equals(data)) {
for (int j = i; j < size; j++)
arrayList[j] = arrayList[j + 1];
flag=true;
size--;
break;
}

}

return flag;

}

@Override
public Iterator iterator() {
Iterator it = new Iterator() {

private int currentIndex = 0;

@Override
public boolean hasNext() {
return currentIndex < size && arrayList[currentIndex] != null;
}

@Override
public Object next() {
return arrayList[currentIndex++];
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return it;
}

public String toString() {
String result = "[";

for (int i = 0; i < size; i++) {

if (i == 0)
result = result + " " + arrayList[i];
else {
result = result + ", " + arrayList[i];
}
}

return result + "]";
}

public static void main(String[] args) {

CustomList demo = new CustomList();
demo.add(new Integer(10));
demo.add(new Integer(20));
demo.add(new Integer(30));
demo.add(new Integer(40));
System.out.println(demo);
}

}
?</code>

let me know if any dought to undertand function. 

Load More

No More Comments