简体   繁体   English

尺寸可变的二维阵列

[英]2D Array with variable size

What I'm trying to accomplish is have something like 我想要完成的是

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

SizeX would be based on the length of a root JSON array, and sizeY based on the length of an array inside the root JSON array. SizeX基于根JSON数组的长度,sizeY基于根JSON数组内部的数组的长度。

Thinking about it, I could use a single array of arrays, but I'm not sure how I'd reference the internal array to get information back from it. 考虑一下,我可以使用单个数组数组,但是我不确定如何引用内部数组以从中获取信息。 For example: 例如:

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

From here, how would I reference the internal array the objects in the internal array? 从这里开始,我将如何引用内部数组中的内部数组中的对象?

The 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
}}

UPDATE: since you updated your post with more info, here is my new answer. 更新:由于您更新了更多信息,所以这是我的新答案。

You don't have a JSON array of JSON arrays, but an array of JSON objects. 您没有JSON数组的JSON数组,但是有JSON对象的数组。 These objects, in turn, have one property that is a map (I believe), but it does not matter. 这些对象又具有一个属性,即地图(我相信),但这并不重要。 You're trying to use a 2D-array instead of a 1D-array of objects. 您正在尝试使用2D数组而非对象的1D数组。

Create a class to represent your objects: 创建一个代表您的对象的类:

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

Then use your class to create the array: 然后使用您的类创建数组:

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);
}

You can then create each mastery page and parse the JSON to fill its fields. 然后,您可以创建每个精通页面并解析JSON以填充其字段。

Declare it like this: 像这样声明:

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

And then add arrays to it by either of the following means: 然后通过以下两种方式向其添加数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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