簡體   English   中英

將2D數組添加到數組的數組列表中

[英]Add A 2D Array to Array List of Arrays

編輯:數組列表聲明:

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

我正在嘗試使用數組列表創建2D數組的數組。

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

其中drawBoxClassObject.drawBox(l); 返回一個二維字符串數組,此行給我這個錯誤:

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)

如何將2D字符串數組本身存儲在1D字符串數組中(我認為這可以解決無法將其轉換為String的問題)?

從以下更改shapeList的聲明

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

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

您的錯誤表明您的shapeList初始化如下:

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

但是,如果要存儲drawBoxClassObject ,則需要更改聲明以接受2D字符串數組,如下所示:

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

這應該工作:

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

您要放置String [] []而不是String。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM