Results 1 to 4 of 4
  1. #1
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default iterator not working properly

    hello, i've written a procedure to paint stuff on the panel, i have an ArrayList of object, and i'm using the iterator for going
    through it. the problem is that at some points in the process, the iterator skips one element, i have no clue as to why that is.
    Java Code:
    private void drawMovingTiles()
        {
            for (Iterator<MovingTile> it = mt.iterator(); it.hasNext();) {
                MovingTile m = it.next();System.out.println("Switch"+m.getX()+" "+m.getY());
                if (!(m.getMtx() >= limitx && m.getMtx() < maxlimitx)) {break;}
                if (!(m.getMty() >= limity && m.getMty() < maxlimity)) {break;}
                for (int x = 0;x<MovingTile.MOVINGTILEW;x++)
                {
                    for (int y = 0;y<MovingTile.MOVINGTILEH;y++)
                    {
                        if (m.getTile()[y][x] != 0)
                        {
                            g2d.setColor(Color.MAGENTA);
                            g2d.drawOval(locx+x+m.getX(),locy+y+m.getY()-MovingTile.MOVINGTILEH,1,1);
                        }
                    }
                }
            }
        }
    i have added the System.out.println code to see the results, and for some reason at some point it keeps printing out
    the first element of the array at every cylce ( i call this procedure from a loop inside the main class.
    can anyone tell me what i'm doing wrong here?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: iterator not working properly

    Can you print out the contents of the arraylist: mt and the output from the posted code that shows what the code is doing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: iterator not working properly

    Java Code:
    mt = (ArrayList) "size = 2"	
    [0] = (MovingTile) #1381	
    UP = (int) -1	
    DOWN = (int) 1	
    LEFT = (int) -1	
    RIGHT = (int) 1	
    x = (int) 144	
    y = (int) 215	
    minxy = (int) 96	
    maxxy = (int) 256	
    mtx = (int) 9	
    mty = (int) 13	
    dir = (int) 1	
    currentdir = (int) -1	
    level = (Level) #1375	
    Static	
    [1] = (MovingTile) #1382	
    UP = (int) -1	
    DOWN = (int) 1	
    LEFT = (int) -1	
    RIGHT = (int) 1	
    x = (int) 524	
    y = (int) 128	
    minxy = (int) 512	
    maxxy = (int) 608	
    mtx = (int) 32	
    mty = (int) 8	
    dir = (int) -1	
    currentdir = (int) -1	
    level = (Level) #1375	
    Static
    this is the variables window at the point where the loop goes through only the first element.

    Java Code:
    Switch513 128
    Switch144 244
    Switch512 128
    Switch144 245
    Switch512 128
    Switch144 246
    Switch513 128
    Switch144 247
    Switch514 128
    Switch144 248
    Switch515 128
    Switch144 249
    Switch516 128
    Switch144 250
    Switch517 128
    Switch144 251
    Switch518 128
    Switch144 252
    Switch519 128
    Switch144 253
    Switch520 128
    Switch144 254
    Switch144 255
    Switch144 256
    Switch144 256
    Switch144 255
    Switch144 254
    Switch144 253
    Switch144 252
    Switch144 251
    Switch144 250
    Switch144 249
    Switch144 248
    Switch144 247
    Switch144 246
    this is a portion of the output, upto the point where it stops aknowledging the second one..

  4. #4
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: iterator not working properly

    nevermind, i figured out what i did wrong.
    it was because of the break statement, whenever the first element was outside of the picture, the break cause the whole function to be aborted.
    not only the current element as i intended.. silly me

Similar Threads

  1. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 08:40 PM
  2. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 03:12 PM
  3. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 07:39 PM
  4. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 09:04 AM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •