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).

2
  • Are you sure you can write it this way? I would expect a () after <String> Commented Oct 4, 2015 at 19:47
  • 1
    Here's a good explanation as to why you can't do what you're trying to do: ibm.com/developerworks/java/library/j-jtp01255/index.html Commented Oct 4, 2015 at 19:51

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.