处理语言:从 ArrayList() 获取内部数组值

发布于 2024-12-09 02:41:44 字数 728 浏览 0 评论 0原文

我正在尝试在处理中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-12-16 02:41:44

您的 ArrayList 应该输入:

List<int[]> list = new ArrayList<int[]>();

如果不是,那么您使用的是原始列表,它可以包含任何内容。因此,其 get 方法返回 Object (这是所有 Java 对象的根类),并且您必须使用强制转换:

int[] point = (int[]) trackPoints.get(5);
println(point[0]);

您应该阅读有关 泛型,并阅读 ArrayList的api文档

Your ArrayList should by typed :

List<int[]> list = new ArrayList<int[]>();

If it's not, then you're using a raw List, which can contain anything. Its get method thus returns Object (which is the root class of all the Java objects), and you must use a cast:

int[] point = (int[]) trackPoints.get(5);
println(point[0]);

You should read about generics, and read the api doc of ArrayList.

聽兲甴掵 2024-12-16 02:41:44

ArrayList 类上的 get() 方法返回一个对象,除非您将其与泛型一起使用。所以基本上当你说 trackPoints.get(5) 时,它返回的是一个对象。

它与,

Object obj = list.get(5);

所以你不能调用obj[0]

为此,您需要先输入大小写,如下所示:

( (int[]) trackPoints.get(5) )[0]

The get() method on ArrayList class returns an Object, unless you use it with generics. So basically when you say trackPoints.get(5), what it returns is an Object.

It's same as,

Object obj = list.get(5);

So you can't call obj[0].

To do that, you need to type case it first, like this:

( (int[]) trackPoints.get(5) )[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文