I am getting an error that says Error: type ArrayToList does not take parameters:…
- User
I am getting an error that says Error: type ArrayToList does not take parameters: ArrayList compacted = new ArrayList<>(); This is my code (Java 8): import java.util.*;
interface MyList { void convert(String[] a); void replace(int idx); ArrayList compact(); }
class InvalidStringException extends Exception { public InvalidStringException(String message) { super(message); } }
class ArrayToList implements MyList { private ArrayList arrayToList = new ArrayList<>();
code@Override public void convert(String[] a) { arrayToList.clear(); for (String str : a) { arrayToList.add(str); System.out.println("I have added the string: " + str + " at the index: " + arrayToList.indexOf(str)); } } @Override public void replace(int idx) { if (idx >= 0 && idx < arrayToList.size()) { String replaced = arrayToList.get(idx); arrayToList.set(idx, “”); System.out.println(“I have replaced the string: " + replaced + " with a null string”); } else { System.out.println("Invalid index: " + idx); } } @Override public ArrayList<String> compact() { ArrayList<String> compacted = new ArrayList<>(); for (String str : arrayToList) { if (!str.isEmpty()) { compacted.add(str); } } arrayToList = compacted; return arrayToList; }}
public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random rand = new Random(0);
code - Assistant