Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a java program named ArrayList.java that create/build the ArrayList data S

ID: 3750393 • Letter: W

Question

write a java program named ArrayList.java that create/build the ArrayList data Structure that exist in the java library. The class must be written to accept any type of Objects. The following must be implemented i.e. YOU must write the code (do not import them from the Java Library): (NO DYNAMIC DATA STRUCTURE MAY BE USED)

One default constructor that will create an ArrayList object with a default size (capacity) of 10 ------ public ArrayList();

Another constructor that accepts a parameter of type int and sets the size to this parameter ---- public ArrayList(int n);

A method that allows you to place a value at the end of the ArrayList ---- public void add(Object x);

A method that allows you to place a value at a given location ---- public void add(int index, Object x);

A method that allows you to retrieve a value from a given location ---- public Object get(int index);

A method that allows you the number of elements in the the ArrayList ---- public int size();

A method would test to see if the ArrayList is empty ---- public boolean isEmpty();

A method that see if a particular object exist in the ArrayList --- public boolean isIn(Object ob);

A method that will return the location of first occurrence of an Object starting from location 0 ----- public int find (Object n);

A method that will remove the first occurrence of an Object starting from location 0 ----- public void remove (Object n);

Now, write a driver program (the class with the public static void main(String[] args) method) name testarray.java to test the ArrayList data structure you just created. That is, you must test all ten (including the default constructor) of the above methods. For those that are returning a value, print out the returned value to the screen and for those that are not returning a value, print a message that indicate if it successfully completed its task.


To test the ArrayList, you must randomly generate 15 integer numbers ranging from 1 to 25 and add to the two ArrayList you created (One using the no-parameter constructor and the other using the one-parameter constructor). Of course, to use the one-parameter constructor, you must prompt the user for an initial size of the ArrayList.

Remember --- The size function should be based upon the actual number of elements you have in the ArrayList at any given time.

Explanation / Answer

ArrayList.java

==============

package com.sky.demo;

import java.util.Arrays;

public class ArrayList<T> {

private int length;

private Object[] arr;

// No Argument Constructor

public ArrayList() {

length=10;

arr = new Object[length];

}

// Parameterized Constructor

public ArrayList(int size) {

this.length = size;

arr = new Object[size];

}

// Method to add object at the end of ArrayList

public void add(T object) {

arr[length-1]=object;

}

// Method to add the value at given index

public void add(int index, T object) {

if(index<length)

arr[index] = object;

else

System.out.println("Entered position is out of range");

}

public T get(int index) throws ArrayIndexOutOfBoundsException{

return (T) arr[index];

}

//Method to get the size of ArrayList

public int size() {

return arr.length;

}

// Method to check ArrayList is empty or not

public boolean isEmpty(){

boolean empty = true;

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

if(!(arr[i]==null)) {

empty = false;

break;

}

}

return empty;

}

// Method to check an Object exist or not in the ArrayList

public boolean isIn(T obj){

boolean exist = false;

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

T obj1 = (T)arr[i];

if(obj1.toString().equals(obj.toString())) {

exist = true;

break;

}

}

return exist;

}

// Method to return position of first occurrence of an object in the ArrayList. If object does not exist in the Arraylist then it will return -1

public int find(T obj) {

int position = -1;

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

T obj1 = (T)arr[i];

if(obj1.toString().equals(obj.toString())) {

position=i;

break;

}

}

return position;

}

// Method to remove the first occurrence of the Object from ArrayList

public void remove(T obj) {

boolean exist = false;

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

T obj1 = (T)arr[i];

if(obj1.toString().equals(obj.toString())) {

exist = true;

arr[i]=null;

break;

}

}

if(exist==false)

System.out.println(obj+" not exist in the ArrayList");

}

@Override

public String toString() {

return Arrays.toString(arr);

}

}

TestArray.java with Default Size

============================

package com.sky.demo;

import java.util.Random;

public class TestArray {

public static void main(String[] args) {

//Creating Object with default size = 10

ArrayList arrayList = new ArrayList();

System.out.println("ArrayList Size = "+arrayList.size());

System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());

//Generating 10 random numbers and adding into the Arraylist

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

Random random = new Random();

int randomNumber = random.nextInt(50)+1;

arrayList.add(i, randomNumber);

}

//Printing the ArrayList

System.out.println("Added 10 random numbers in the arrayList");

System.out.println("ArrayList : "+arrayList);

System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());

//Adding 200 in the last of ArrayList

arrayList.add(200);

System.out.println("ArrayList : "+arrayList);

// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown

int element = (int) arrayList.get(9);

System.out.println("Element = "+element);

System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));

// Finding first occurrence of an Object

int pos = arrayList.find(200);

if(pos>=0)

System.out.println("Element exist at position : "+pos);

else

System.out.println("Element does not exist");

arrayList.remove(201);

System.out.println("Arraylist = "+arrayList);

}

}

TestArray.java with user input

=======================

package com.sky.demo;

import java.util.Random;

import java.util.Scanner;

public class TestArray {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the Size : ");

int len = in.nextInt();

//Creating Object with default size = 10

ArrayList arrayList = new ArrayList(len);

System.out.println("ArrayList Size = "+arrayList.size());

System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());

//Generating 10 random numbers and adding into the Arraylist

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

Random random = new Random();

int randomNumber = random.nextInt(50)+1;

arrayList.add(i, randomNumber);

}

//Printing the ArrayList

System.out.println("Added 10 random numbers in the arrayList");

System.out.println("ArrayList : "+arrayList);

System.out.println("Is ArrayList Empty = "+arrayList.isEmpty());

//Adding 200 in the last of ArrayList

arrayList.add(200);

System.out.println("ArrayList : "+arrayList);

// If you will enter the index > size java.lang.ArrayIndexOutOfBoundsException will be thrown

int element = (int) arrayList.get(9);

System.out.println("Element = "+element);

System.out.println("Value exist in the ArrayList = "+arrayList.isIn(200));

// Finding first occurrence of an Object

int pos = arrayList.find(200);

if(pos>=0)

System.out.println("Element exist at position : "+pos);

else

System.out.println("Element does not exist");

arrayList.remove(201);

System.out.println("Arraylist = "+arrayList);

}

}