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<PVector> path = new ArrayList<>();
Now its empty, later in the Process it fills with PVector entrys :
{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}
....
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)[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}
?
So you have a ArrayList
of PVector
s, right? This means, when you get
from the ArrayList
, you get an PVector
back. I do not know PVector, but there is (hopefully) a method in PVector
to get the first int ( x()
or something).
For questions like these, the reference is your best friend.
But remember that path.get(0)
returns a PVector
. You can then use the PVector API to get at its position. 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. The p
variable isn't necessary; I'm just using it to show that path.get()
returns a 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.