my dog learned polymorphism
The moose likes Beginning Java and the fly likes Problem with for-each loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with for-each loop" Watch "Problem with for-each loop" New topic

Problem with for-each loop

Pan Niko
Ranch Hand

Joined: Mar 19, 2011
Posts: 86

Is the first time that im using for-each loop so i dont know what im doing wrong. I have a 2-D array that i want to scan to find some elements. So i create a class Coordinate() that takes two integers (row, column) and addCoordinate() method where each variable added to itself.

Pawn starting always at the bottom of theArray (the last row of the array) For example if is starting on theArray[15][2] (Stone) and at theArray[10][2] there is another Stone and between them there ara Bridge elements then sets

but if standing on theArray[10][2] then

So starting and ending points are the same object. I cant understand why?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

I'm not sure I understand your question that clearly, is there anyway you could post an example of your problem that compiles so we can test it. Also you are not using a 2d array, you are using a 1d array that has objects inside it.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
That sounds as though you should provide bindings from one Coordinate to the next. It looks almost as though you had a tree, rather than an array. You should traverse trees differently from arrays.
I agree. Here's the link:
subject: Problem with for-each loop
It's not a secret anymore!