1

I get the following error in my IDE "generic array creation"

I googled it but found very long explanations and didn't quite understand what the best solution is to this problem.

If anyone could suggest the best solution to this to get my code to compile...

public ArrayList<String>[] getClosedTicketIDs(Account account) {

    ArrayList<String> closedSourceTickets = new ArrayList<>();
    ArrayList<String> closedAccountTickets = new ArrayList<>();

    // ...some unimportant to this example code...

    // return
    ArrayList<String>[] a = new ArrayList<String>[2]; // <-- generic array creation error
    a[0] = closedSourceTickets;
    a[1] = closedAccountTickets;
    return a;
}

My objective is to return an array consisting of 2 ArrayList<String> (no more, no less).

Nayuki
  • 16,655
  • 5
  • 47
  • 75
ycomp
  • 6,614
  • 15
  • 50
  • 82
  • Are you sure you can write it this way? I would expect a () after – Yassin Hajaj Oct 04 '15 at 19:47
  • 1
    Here's a good explanation as to *why* you can't do what you're trying to do: http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html – paulsm4 Oct 04 '15 at 19:51

2 Answers2

2

You can only create raw array types. You need to do this: a = new ArrayList[2];

Nayuki
  • 16,655
  • 5
  • 47
  • 75
1

You cant do that but you can do

List<List<String>> a=new ArrayList<ArrayList<String>>();

but the better would be

ArrayList[] a=new ArrayList[n];

as you can fix the size in this.

singhakash
  • 7,613
  • 4
  • 25
  • 59