简体   繁体   中英

Add A 2D Array to Array List of Arrays

Edit: Array List declaration:

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

I'm trying to create an array of 2D arrays using an array list.

shapeList.add(drawBoxClassObject.drawBox(l));

Where drawBoxClassObject.drawBox(l); returns a 2D string array, this line gives me this error:

no suitable method found for add(String[][])
method Collection.add(String) is not applicable
  (argument mismatch; String[][] cannot be converted to String)
method List.add(String) is not applicable
  (argument mismatch; String[][] cannot be converted to String)

How can I store the 2D string array itself inside a 1D string array (which I think would fix the cannot be converted to String)?

Change the declaration of shapeList from

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

to

List<String[][]> shapeList = new ArrayList<>();

Your error shows that your shapeList is initialized somewhat like this:

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

But if you want to store drawBoxClassObject , then you need to change declaration to accept 2D string array, like this:

List<String[][]> shapeList = new ArrayList<>();

This should work:

    List<String[][]> shapeList = new ArrayList<String[][]>();

You are putting a String[][] not a String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM