简体   繁体   中英

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.

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

{"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. 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.

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.

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

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