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<>();
@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 compact() { ArrayList 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);
int n = Integer.parseInt(sc.nextLine()); String[] a = new String[n];
for(int i = 0; i < n; i++) a[i] = sc.nextLine();
MyList obj = new ArrayToList();
obj.convert(a); int x = rand.nextInt(n); for(int i = 0; i < x; i++) obj.replace(rand.nextInt(n));
ArrayList s = obj.compact();
for (int i = 0; i < s.size(); i++){ if(s.get(i).charAt(0) >= 97 && s.get(i).charAt(0) <= 122){ try{ throw new InvalidStringException("This is an invalid string"); } catch(InvalidStringException e){System.out.println(e.getMessage());} } }
} }
- Assistant