In: Computer Science
Submit your complete implementation of our abstract list type using an array, ArrayList.java.
public class ArrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();
}
private String[]a = new String[1000];
private int end = -1;
// Throw an IndexOutOfBoundsException if the index is invalid public String get(int index); // Return the first index in the list with the given value, or -1 if it's not found public int find(String val); // Return true if the element is successfully added, false if the list is full public boolean add(String val); // Throw an IndexOutOfBoundsException if the index is invalid public void add(int index, String val); // This method should return true only if the value is in the list and removed, // false if the value isn't found in the list public boolean remove(String val); // return the value that's removed; throw an IndexOutOfBoundsException if the index is invalid public String remove(int index); // LENGTH() → int public int size(); // Return true if the list is empty, false if it's not public boolean isEmpty(); // Return true if the value is in the list, false if it's not public boolean contains(String value); // DELETE_ALL() public void clear(); // Convert the contents into a single string with values separated by commas. // It's OK to have a comma at the end of the String. public String toString();
Program:
//ArrayList class
class ArrayList
{
   //main method
   public static void main(String[] args)
   {
       ArrayList list = new
ArrayList();
      
       list.add("C");
       list.add("C++");
       list.add("C#");
       list.add("Java");
       list.add("Python");
      
       System.out.println(list);
      
       System.out.println("Size = " +
list.size());
      
       System.out.println("Is empty = " +
list.isEmpty());
      
      
System.out.println(list.get(2));
      
      
System.out.println(list.contains("C#"));
      
       list.remove(2);
       list.remove("Python");
      
      
System.out.println(list.contains("C#"));
      
       System.out.println(list);
      
       list.clear();
      
       System.out.println(list);
      
       System.out.println("Is empty = " +
list.isEmpty());
   }
   //private members
   private String[]a = new String[1000];
   private int end = -1;
    // Throw an IndexOutOfBoundsException if the
index is invalid
   public String get(int index)
   {
       if(index<0 || index
>=a.length)
           throw new
IndexOutOfBoundsException();
      
       return a[index];
   }
   // Return the first index in the list with the
given value, or -1 if it's not found
   public int find(String val)
   {  
       for(int i=0; i<=end; i++)
       {
          
if(a[i].equals(val))
          
    return i;
       }
      
       return -1;
   }
   // Return true if the element is successfully
added, false if the list is full
   public boolean add(String val)
   {
       if(end+1 >= a.length) return
false;
       end++;
       //add the value
       a[end] = val;
       return true;
   }
   // Throw an IndexOutOfBoundsException if the index
is invalid
   public void add(int index, String val)
   {
       if(index<0 || index
>=a.length)
           throw new
IndexOutOfBoundsException();
       a[index] = val;
   }
   // This method should return true only if the value
is in the list and removed,
   // false if the value isn't found in the list
   public boolean remove(String val)
   {
       int f = find(val);
       if(f==-1)
           return
false;
       //remove the value
       for(int i=f; i<end; i++)
           a[i] =
a[i+1];
       end--;
       return true;
   }
// return the value that's removed; throw an
IndexOutOfBoundsException if the index is invalid
public String remove(int index)
{
       if(index<0 || index >
end)
           throw new
IndexOutOfBoundsException();
          
       String s = a[index];
       //remove the value
       for(int i=index; i<end;
i++)
           a[i] =
a[i+1];
       end--;
       return s;
}
// LENGTH() → int
public int size()
{
   return (end + 1);
}
// Return true if the list is empty, false if it's not
public boolean isEmpty()
{
   if(end==-1) return true;
  
   return false;
}
// Return true if the value is in the list, false if it's
not
public boolean contains(String value)
{
   int i = find(value);
   if(i==-1) return false;
   return true;
}
// DELETE_ALL()
public void clear()
{
   end = -1;
}
// Convert the contents into a single string with values
separated by commas.
// It's OK to have a comma at the end of the String.
public String toString()
{
   String str = "[]";
   if(end==-1) return str;
  
   str = "[";
   for(int i=0; i<end; i++)
   {
       str = str + a[i] + ", ";
   }
   str = str + a[end] + "]";
  
   return str;
}
}
Output:
[C, C++, C#, Java, Python]
Size = 5
Is empty = false
C#
true
false
[C, C++, Java]
[]
Is empty = true
N.B: Whether you face any problem then share with me in the
comment section, I'll happy to help you. I expect positive feedback
from your end.