2D数组,大小可变

时间:2014-05-05 14:29:53

标签: java arrays

我想要完成的事情就像是

String[][] array = new String[sizeX][sizeY]

SizeX将基于根JSON数组的长度,而sizeY将基于根JSON数组内的数组长度。

考虑到这一点,我可以使用单个数组阵列,但我不确定我如何引用内部数组以从中获取信息。例如:

String[] array = new String[sizeX]; 
array[i] = new String[sizeY]; // initialized to variable size of JSON

从这里开始,我如何引用内部数组中内部数组的对象?

JSON

{"23591778": {
   "pages": [
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787712,
         "name": "Blind",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4251,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 3
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4241,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787713,
         "name": "AD/Tank Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4242,
               "rank": 1
            },
            {
               "id": 4233,
               "rank": 3
            },
            {
               "id": 4243,
               "rank": 1
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4213,
               "rank": 2
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4252,
               "rank": 2
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4123,
               "rank": 3
            },
            {
               "id": 4262,
               "rank": 1
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4133,
               "rank": 1
            },
            {
               "id": 4234,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            }
         ],
         "id": 34787714,
         "name": "Mumu",
         "current": true
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4144,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4111,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787715,
         "name": "vi/j4/wuk/xin/noc",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4353,
               "rank": 3
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4311,
               "rank": 1
            },
            {
               "id": 4362,
               "rank": 1
            },
            {
               "id": 4322,
               "rank": 3
            },
            {
               "id": 4334,
               "rank": 1
            },
            {
               "id": 4332,
               "rank": 1
            },
            {
               "id": 4352,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4314,
               "rank": 1
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4331,
               "rank": 3
            },
            {
               "id": 4324,
               "rank": 1
            },
            {
               "id": 4313,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4342,
               "rank": 1
            },
            {
               "id": 4341,
               "rank": 1
            }
         ],
         "id": 34787716,
         "name": "Support",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4214,
               "rank": 2
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4113,
               "rank": 4
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4224,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 3
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            }
         ],
         "id": 34787717,
         "name": "AD Jungle",
         "current": false
      },
      {
         "masteries": [
            {
               "id": 4212,
               "rank": 2
            },
            {
               "id": 4211,
               "rank": 2
            },
            {
               "id": 4121,
               "rank": 1
            },
            {
               "id": 4134,
               "rank": 3
            },
            {
               "id": 4124,
               "rank": 1
            },
            {
               "id": 4114,
               "rank": 1
            },
            {
               "id": 4222,
               "rank": 3
            },
            {
               "id": 4112,
               "rank": 4
            },
            {
               "id": 4221,
               "rank": 1
            },
            {
               "id": 4122,
               "rank": 3
            },
            {
               "id": 4152,
               "rank": 3
            },
            {
               "id": 4141,
               "rank": 1
            },
            {
               "id": 4142,
               "rank": 1
            },
            {
               "id": 4132,
               "rank": 1
            },
            {
               "id": 4232,
               "rank": 1
            },
            {
               "id": 4162,
               "rank": 1
            },
            {
               "id": 4131,
               "rank": 1
            }
         ],
         "id": 34787718,
         "name": "ADC",
         "current": false
      },
   ],
   "summonerId": 23591778
}}

2 个答案:

答案 0 :(得分:1)

更新:,因为您使用更多信息更新了帖子,这是我的新答案。

您没有JSON数组的JSON数组,而是JSON对象的数组。反过来,这些对象有一个属性是地图(我相信),但无所谓。您正在尝试使用2D数组而不是1D对象数组。

创建一个表示对象的类:

public class MasteryPage {
    private long id;
    private String name;
    private boolean current;
    private Map<Long,Integer> masteries = new HashMap<>();

    public MasteryPage(long id, String name, boolean current) {
        this.id = id;
        this.name = name;
        this.current = current;
    }

    public void addMastery(long id, int rank) {
        masteries.put(id, rank);
    }

    // add getters/setters...      
}

然后使用您的类创建数组:

JSONArray jArray = json.getJSONArray("pages");
MasteryPage[] arr = new MasteryPage[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
    JSONObject jsonMasteryPage = jArray.getJSONObject(i);
    long id = jsonMasteryPage.getLong("id");
    String name = jsonMasteryPage.getString("name");
    boolean current = jsonMasteryPage.getBoolean("current");
    MasteryPage page = new MasteryPage(id, name, current);
    JSONArray jsonMasteries = jsonMasteryPage.getJSONArray("masteries");
    for (int j = 0; j < jsonMasteries.length(); j++) {
        JSONObject jsonMastery = jsonMasteries.getJSONObject(i);
        page.addMastery(jsonMastery.getLong("id"), jsonMastery.getInt("rank"));
    }
    arr.add(page);
}

然后,您可以创建每个掌握页面并解析JSON以填充其字段。

答案 1 :(得分:0)

声明如下:

String[][] arr = new String[][numRows];

然后通过以下任一方式为其添加数组:

arr[0] = new String[lengthOfJSONArray];
arr[0] = getStringArray();