gpt4 book ai didi

java - 可变大小的二维数组

转载 作者:行者123 更新时间:2023-12-01 13:01:42 25 4
gpt4 key购买 nike

我想要完成的是类似的事情

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

最佳答案

更新:由于您用更多信息更新了帖子,这是我的新答案。

您没有 JSON 数组的 JSON 数组,而是 JSON 对象的数组。反过来,这些对象有一个属性,那就是 map (我相信),但这并不重要。您正在尝试使用 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 以填充其字段。

关于java - 可变大小的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23474984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com