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

You are to write a program name Arraylist.java that implements the ArrayList dat

ID: 669590 • Letter: Y

Question

You are to write a program name Arraylist.java that implements 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):

1.

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

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

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

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

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

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

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

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

10.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.

Explanation / Answer

public class testarray{
  
   public static void main(String args[]){
   Arraylist a=new Arraylist();
   System.out.println("Size of array list: "+a.size());
   Arraylist a1=new Arraylist(4);
   a1.add(3);
   System.out.println("Element at the end: "+a1.get(0));
   a1.add(0, 0);
   a1.add(1, 1);
   a1.add(2, 2);
   System.out.println("Is the arraylist empty: "+a1.isEmpty());
  
   System.out.println("find 2 in list"+a1.find(2));
   a1.remove(1);
   System.out.println("Is 1 in list: "+a1.isIn(1));
   }
}

import java.util.ArrayList;


public class Arraylist {
   ArrayList<Object> a;
   public Arraylist() {
       a=new ArrayList<Object>(10);
   }
public Arraylist(int n){
   a=new ArrayList<Object>(n);
}
public void add(Object x){
   a.add(x);
}
public void add(int index,Object x){
   a.add(index,x);
}
public Object get(int index){
   return a.get(index);
}
public int size(){
   return a.size();
}
public boolean isEmpty(){
   return a.isEmpty();
}
public boolean isIn(Object ob){
   return a.contains(ob);
}
public int find(Object n){
   int index=0;
   for(int i=0;i<a.size();i++){
       if(a.get(i).equals(n)){
           index=i;
       }
   }
   return index;
}
public void remove(Object n){
   a.remove(n);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote