处理语言:从 ArrayList() 获取内部数组值
我正在尝试在处理中使用 ArrayList() 方法。
我有这个:
ArrayList trackPoints = new ArrayList();
//inside a loop
int[] singlePoint = new int[3];
singlePoint[0] = 5239;
singlePoint[1] = 42314;
singlePoint[2] = 1343;
//inside a loop
trackPoints.add(singlePoint);
所以基本上我想向我的 ArrayList 添加一个具有三个值的数组“singlePoint”。
这似乎工作正常,因为现在我可以使用 println(trackPoints.get(5));
并且我得到了这个:
[0] = 5239;
[1] = 42314;
[2] = 1343;
但是我怎样才能获得这个数组的单个值?
println(trackPoints.get(5)[0]);
不起作用。
我收到以下错误: “表达式的类型必须是数组类型,但它解析为对象”
知道我做错了什么吗?如何从这个包含多个数组的 arrayList 中获取单个值?
感谢您的帮助!
I'm trying to use the ArrayList() method in Processing.
I have this:
ArrayList trackPoints = new ArrayList();
//inside a loop
int[] singlePoint = new int[3];
singlePoint[0] = 5239;
singlePoint[1] = 42314;
singlePoint[2] = 1343;
//inside a loop
trackPoints.add(singlePoint);
So basically I want to add an array "singlePoint" with three values to my ArrayList.
This seems to work fine, because now I can use println(trackPoints.get(5));
and I get this:
[0] = 5239;
[1] = 42314;
[2] = 1343;
However how can I get a single value of this array?
println(trackPoints.get(5)[0]);
doesn't work.
I get the following error:
"The type of the expression must be an array type but it resolved to Object"
Any idea what I'm doing wrong? How can I get single values from this arrayList with multiple arrays in it?
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
ArrayList
应该输入:如果不是,那么您使用的是原始列表,它可以包含任何内容。因此,其
get
方法返回Object
(这是所有 Java 对象的根类),并且您必须使用强制转换:您应该阅读有关 泛型,并阅读 ArrayList的api文档。
Your
ArrayList
should by typed :If it's not, then you're using a raw List, which can contain anything. Its
get
method thus returnsObject
(which is the root class of all the Java objects), and you must use a cast:You should read about generics, and read the api doc of ArrayList.
ArrayList 类上的
get()
方法返回一个对象,除非您将其与泛型一起使用。所以基本上当你说trackPoints.get(5)
时,它返回的是一个对象。它与,
所以你不能调用
obj[0]
。为此,您需要先输入大小写,如下所示:
The
get()
method on ArrayList class returns an Object, unless you use it with generics. So basically when you saytrackPoints.get(5)
, what it returns is an Object.It's same as,
So you can't call
obj[0]
.To do that, you need to type case it first, like this: