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

public class StringList {//an array that maintains a list of String objects//the

ID: 3812068 • Letter: P

Question

public class StringList {//an array that maintains a list of String objects//the default initial size of the array is 10 private String[] strings;//the number of valid String objects stored in the//array of strings private int count;} Implement a method removeLast that takes as input a String and removes the last occurrence of that String in the list. lf the String does not appear in the list the method does nothing. You may not rely on any other methods of the class. The following shows the state of the instances variables before and after a call to removeLast with the input "cat" Before: strings rightarrow {"cat", "dog", "cat", "bird", "cat", "alpaca", null, null, null, null}, count=6 After: strings rightarrow {"cat", "dog", "cat", "bird", "alpaca", null, null, null, null, null), count=5 It is also fine if you leave the item at position 5 as "alpaca" as long as count is updated correctly.

Explanation / Answer

HI, Please find my implementation.Please let me know in case of any issue.

public class StringList {

   private String[] strings;

   private int count;

   public StringList() {

       strings = new String[10]; // default size

       count = 0;

   }

   public void removeLast(String str){

       if(count == 0)

           return;

       int maxIndex = -1;

       for(int i=0; i<count; i++)

           if(strings[i].equals(str))

               maxIndex = i;

       if(maxIndex != -1){

           // if it is the last element in array

           if(maxIndex == count-1){

               strings[count-1] = null;

               count = count -1;

           }else{

               for(int i=maxIndex; i<count-1; i++){

                  

                   strings[i] = strings[i+1];

               }

              

               strings[count-1] = null;

               count--;

           }

       }

   }

}