本文介绍了大小未知的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个未定义大小的数组,该数组将包含用户信息.

I need to create an array with an undefined size, which will contain user information.

例如:

user[0]["name"] = "Patrick";

但是,标准的Java数组似乎需要已知的长度,我不知道.

However, the standard java array seems to require a known length, which I don't know.

我可以使用什么替代方法?(我也很高兴看到一些编码示例)我正在使用TCP框架( kryonet )不允许使用构造函数传递对象.因此,据我所知,不可能创建User对象.

What alternative can i use? (I'll gladly see some coding examples as well) I'm using a TCP framework (kryonet) which doesn't allow to pass objects with constructors. Therefore, as far as i can see, making a User object is not possible.

推荐答案

在Java中,我们只能对单元格索引使用整数值.

In java we can only use integer values for cell indexes.

我想您正在寻找地图列表:

I guess you're looking for a list of maps:

List<Map<String, String>> persons = new ArrayList<Map<String, String>>();

// add Patrick
Map<String, String> person = new HashMap<String, String>();
person.put("name", "Patrick");
person.put("age", "23");
persons.add(person);

// add Sue
person = new HashMap<String, String>();
person.put("name", "Sue");
person.put("age", "21");
persons.add(person);

要访问所有名称,例如:

To access all names, for instance:

for (Map<String, String> person:persons) {
  System.out.println(person.get("name"));
}

这篇关于大小未知的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:18