Java的:字符串数组的ArrayList中 [英] Java: ArrayList of String Arrays

查看:141
本文介绍了Java的:字符串数组的ArrayList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿做这样的事情:

    private ArrayList<String[]> addresses = new ArrayList<String[3]>();

...这似乎并没有工作。什么存储与每个地址3场阵列中的多个地址,而不为它创建一个单独的类的最简单的方法?

...this does not seem to work. Whats the easiest way of storing multiple addresses with 3 fields per address in an array without creating a separate class for it?

推荐答案

使用第二ArrayList中的3串,不是原始数组。 IE浏览器。结果
私人列表&LT;名单,LT;弦乐&GT;&GT;地址=新的ArrayList&LT;名单,LT;弦乐&GT;&GT;();

Use a second ArrayList for the 3 strings, not a primitive array. Ie.
private List<List<String>> addresses = new ArrayList<List<String>>();

然后,你可以有:

ArrayList<String> singleAddress = new ArrayList<String>();
singleAddress.add("17 Fake Street");
singleAddress.add("Phoney town");
singleAddress.add("Makebelieveland");

addresses.add(singleAddress);

(我觉得有些奇怪的事情可以用类型擦除发生在这里,但我不认为它应该在此问题)

(I think some strange things can happen with type erasure here, but I don't think it should matter here)

如果你使用的基本数组死心塌地,只是一个小变化,需要得到您的示例工作。在其他的答案所解释的,该阵列的尺寸不能被包括在声明。因此更改:

If you're dead set on using a primitive array, only a minor change is required to get your example to work. As explained in other answers, the size of the array can not be included in the declaration. So changing:

private ArrayList<String[]> addresses = new ArrayList<String[3]>();

private ArrayList<String[]> addresses = new ArrayList<String[]>();

将工作。

这篇关于Java的:字符串数组的ArrayList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆