Text Share Online

A ‘List’ is a dynamic array. Convert a static array to a dynamic list. Create the following classes:

1. The InvalidStringException class should inherit the inbuilt Exception class. It should also implement the following methods:

  • InvalidStringException( ): calls the constructor of the parent class.

 

2. The ArrayToList class should implement the interface MyList. It should have one class variable of type ArrayList<String>, arrayToList. It should also implement the following methods:

  • ArrayToList( ): initializes the empty list arrayToList
  • void convert(String[] a):  adds the strings in the array a, to the list arrayToList. Also prints “I have added the string: {string} at the index: {index}“. For example if the string “abcd” is added at the index 0, it should print “I have added the string: abcd at index: 0”.
  • void replace(int idx): replaces the string at the index idx of arrayToList, with an empty string. Also prints “I have replaced the string: {string} with a null string“. For example if the string “abcd” is replaced with the empty string, it should print “I have replaced the string: abcd with a null string”.
  • ArrayList<String> compact(): It should remove all the empty string from the list and return the resulting list.

 

Note: The locked stub code in the editor provides the definition of the interface MyList . It also validates the implementation of InvalidStringException and ArrayToList classes.

 

Constraints

  • 1 ≤ n ≤ 100
  • 1 ≤ length(a[i] ) ≤ 5
Share This: