Time |
S |
Nick |
Message |
00:27 |
|
|
rebecca joined ##friendlyjava |
01:45 |
|
|
bunsen joined ##friendlyjava |
01:46 |
|
bunsen |
Hey there - does anyone know how to access object methods while iterating through a vector of objects? |
01:47 |
|
aditsu |
hi, if you know what type they are, you can cast them and call the methods |
01:48 |
|
bunsen |
It's a vector of a custom class (I'm making a card game). Trying to check if a card isWild, but I'm having trouble accessing the vector elements while iterating through it. Do you have an example? I feel pretty dense. Sorry. |
01:49 |
|
bunsen |
I thought maybe using an enumerator or an iterator would let me call functions through that ... doesn't seem to work, though. |
01:49 |
|
aditsu |
if it's a generic vector: for (Card c : v) if (c.isWild()) {...} |
01:49 |
|
aditsu |
btw, any reason for using Vector instead of ArrayList? |
01:50 |
|
bunsen |
It's for a class - the professor set up vectors as the base, so it's what we're stuck with. Most websites I go to mention ArrayList. Argh! |
01:51 |
|
aditsu |
maybe your professor is stuck in 1998 :p |
01:51 |
|
bunsen |
He's definitely an "OG," as they'd say. Insanely smart, but this class is a bit hard to follow. |
01:52 |
|
aditsu |
if it's not generic, you probably need to do: for (Object o : v) {Card c = (Card) o; if (c.isWild()) {...}} |
01:53 |
|
aditsu |
that's the casting I was talking about |
01:53 |
|
bunsen |
So if I have a vector called tempHand, and it holds cards (so Vector<Card> tempHand), let me see ... |
01:53 |
|
aditsu |
oh, it is generic |
01:53 |
|
aditsu |
use the simple version then |
01:54 |
|
bunsen |
Yeah, and I come from a mainly C++ background so I'm trying to pick this up fast, but tripping over my feet a bit. Digging it, though. |
01:54 |
|
aditsu |
it's a lot like C++ templates |
01:55 |
|
aditsu |
with some differences |
01:56 |
|
bunsen |
so when iterating through the vector, would it be something like: for(tempHand c : v) etc? |
01:56 |
|
bunsen |
Also, thank you so much for this. |
01:56 |
|
aditsu |
it would be for (Card c : tempHand) |
01:57 |
|
bunsen |
You're amazing. |
01:57 |
|
aditsu |
haha, not really, this is simple stuff |
01:57 |
|
aditsu |
there are also other ways to iterate, but this is the easiest |
02:00 |
|
bunsen |
Alright, I think this points me in the right direction. I'm getting a NullPointerException when I run it, but I'm sure that's something I can figure out in no time. |
02:25 |
|
bunsen |
Ok, here's another interesting one. Is there any way to trim a vector as I iterate through it? Say I want to remove all the wildcards from the hand I'm working on, and move them into a new hand. I assume I'd need to mark their spots and remove them from back to front. |
02:25 |
|
bunsen |
But perhaps java has some super simple way of doing that, that I don't know about. |
02:26 |
|
aditsu |
that's a little tricky, as normally you're not supposed to modify the collection while iterating it, but you can actually do that if you use an iterator |
02:26 |
|
aditsu |
so you can iterate using an iterator, then call remove() on the iterator |
02:27 |
|
bunsen |
If iterating front to back I'm assuming that could be catastrophic though? |
02:27 |
|
aditsu |
no, it can handle it |
02:28 |
|
bunsen |
Oh, wow, that's neat. |
02:29 |
|
bunsen |
when it moves to iterate to the next entry after removing, wouldn't it skip over one? Say we're on entry 5, and delete it to move onto entry 6. Is java that awesome that it knows to stay where it is, so to speak, instead of jumping from 5 to 7? |
02:29 |
|
bunsen |
This was something that C++ gave me an issue with, when I made the project there. |
02:31 |
|
aditsu |
it knows |
02:32 |
|
bunsen |
That's wild! (no pun intended) |
02:33 |
|
bunsen |
(ok, pun intended (wild cards and all)) |
02:33 |
|
aditsu |
haha |
02:36 |
|
bunsen |
so if I'm doing the (Card c:tempHand), and I find a wild card, would I do tempHand.remove(c)? That seems to throw an error. |
02:36 |
|
aditsu |
no, tempHand is the vector, you need an iterator |
02:36 |
|
aditsu |
you have to replace the for loop |
02:37 |
|
bunsen |
Ahhhh I see. Yeah, that was the issue I was running into before. I couldn't pull the card's methods through the iterator for some weird reason. Let me take another look at it now though, since you helped me understand it a bit better |
02:43 |
|
aditsu |
bunsen: check out the second example in this answer: https://stackoverflow.com/a/13847716/179864 |
02:43 |
|
aditsu |
replace Bomb with Card, etc |
02:45 |
|
aditsu |
or the next answer in that question |
02:45 |
|
bunsen |
Excellent - thank you! |
02:54 |
|
bunsen |
Works like a charm. I'll pay this forward, once I become a java powerhouse. |
02:55 |
|
aditsu |
cool :) |
04:01 |
|
|
bunsen left ##friendlyjava |
07:35 |
|
|
mr_lou joined ##friendlyjava |
19:23 |
|
|
aditsu_ joined ##friendlyjava |
23:36 |
|
|
Guest75243 joined ##friendlyjava |