[英]Processing/Java ArrayList & LinkedList exception
I am trying to implement a pathfind system into one of my games, So i got following problem. 我试图在我的一款游戏中实现寻路系统,所以我遇到了以下问题。
I got here a nice ArrayList : 我在这里得到了一个不错的ArrayList:
ArrayList<PVector> path = new ArrayList<>();
Now its empty, later in the Process it fills with PVector entrys : 现在为空,稍后在Process中填充PVector条目:
{5.0,6.0,0},{5.0,7.0,0},{5.0,8.0,0},{5.0,9.0,0}
Thats nice isnt it ? 那很好吗? But i cant work with it, because i need only the 5.0 out of the
{5.0,6.0,0}
.... 但是我不能使用它,因为我只需要
{5.0,6.0,0}
的5.0...。
i tried it with path.get(0)
... there i only get {5.0,6.0,0}
... so i found something new this here : 我用
path.get(0)
试了一下...我只得到{5.0,6.0,0}
...所以我在这里找到了一些新的东西:
path.get(0)[0];
that didnt worked either... because the expression type needs to be an array but its resolved to an object 还是没有用...因为表达式类型需要是一个数组,但是它解析为一个对象
So how do i get an single entry out of an index ? 那么,如何从索引中获取单个条目? :/ How do i get
5.0
out of {5.0,6.0,0}
? :/如何从
{5.0,6.0,0}
获得5.0
?
So you have a ArrayList
of PVector
s, right? 所以您有一个
PVector
的ArrayList
,对吗? This means, when you get
from the ArrayList
, you get an PVector
back. 这意味着,当您从
ArrayList
get
时,您将获得一个PVector
。 I do not know PVector, but there is (hopefully) a method in PVector
to get the first int ( x()
or something). 我不知道PVector,但有(希望)的方法
PVector
拿到第一INT( x()
或东西)。
For questions like these, the reference is your best friend. 对于此类问题, 参考文献是您最好的朋友。
But remember that path.get(0)
returns a PVector
. 但是请记住
path.get(0)
返回一个PVector
。 You can then use the PVector API to get at its position. 然后,您可以使用PVector API定位其位置。 Something like this:
像这样:
ArrayList<PVector> path = new ArrayList<PVector>();
//add PVectors to path
PVector p = path.get(0);
float x = p.x;
Notice that I'm using <PVector>
generics so that the ArrayList
knows what types of objects it holds. 请注意,我正在使用
<PVector>
泛型,以便ArrayList
知道其拥有的对象类型。 The p
variable isn't necessary; p
变量不是必需的; I'm just using it to show that path.get()
returns a PVector
. 我只是用它来显示
path.get()
返回一个PVector
。 You could do it in one line as well: 您也可以一行完成:
ArrayList<PVector> path = new ArrayList<PVector>();
//add PVectors to path
float x = path.get(0).x;
When declaring variables, always parametrize the generic types with the most specific type you will store in it: 声明变量时,请始终对将要存储在其中的最特殊类型的泛型类型进行参数化:
// Declaration:
List<PVector> path = new ArrayList<PVector>();
// Storing:
path.add(new PVector(...));
path.add(new PVector(...));
...
// Reading:
PVector pVector=path.get(n);
pVector.get(...)
In this way, when reading items from your list, you will get exactly the same type of object you stored. 这样,从列表中读取项目时,您将获得与存储的对象完全相同的类型 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.