Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Question [Problem] Enhanced for-loop with 2D arrays (or array in array)

    SOLVED : I m stupid I had to write this line for(int i=0;i<=forbiddenloc.length-1;i++) {
    Because the array starts from 0 and length starts counting from 1 I guess.
    Java Code:
    	public void keyPressed(KeyEvent e) {
    			boolean allowedposition=true;
    			int keyCode = e.getKeyCode();
    			if(keyCode == KeyEvent.VK_ESCAPE) {
    				stop();
    			}else if(keyCode == KeyEvent.VK_DOWN && y<=704){
    			for(int i=0;i<=forbiddenloc.length;i++) {
    					i[U]f(x==Integ[/U]er.parseInt(forbiddenloc[i][0]) && y+32==Integer.parseInt(forbiddenloc[i][1])) {
    						allowedposition=false;
    					}
    				}
    				if(allowedposition==true) {
    					y+=32;
    				}
                          }
                          ...
                 }
    Anyway , if someone else has a better way to go trough 2D-array with a loop , please share ...
    Last edited by thewrongsyntax; 10-07-2012 at 09:02 PM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    204
    Rep Power
    0

    Default Re: [Problem] Enhanced for-loop with 2D arrays (or array in array)

    You are going through it right, but let me explain the problem, length indicates how many items are in a collection, index starts counting at 0, so an array with a length of 50 is really only 0-49. So your for loop counts to '<=' length. You are looking for something in the 50th index of an array, which doesn't exist. You need to be using '<'. But subtracting one from the length works as well.

    EDIT: By the way, this is not an advanced for loop, and advanced for loop only allows you to iterate through a collection from start to finish and does not provide index. Here is an example:

    Java Code:
    String[] lines = new String[10];
    
    for (String line : lines) {
    //Stuff
    }
    Last edited by zFollette; 12-25-2013 at 06:26 AM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,735
    Rep Power
    7

    Default Re: [Problem] Enhanced for-loop with 2D arrays (or array in array)

    Do you realize this thread is over a year old? I doubt the OP still needs to resolve this.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: [Problem] Enhanced for-loop with 2D arrays (or array in array)

    Quote Originally Posted by jim829 View Post
    Do you realize this thread is over a year old? I doubt the OP still needs to resolve this.
    I'm closing it before this zombie thread is resurrected again.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Enhanced For Loop Array List Reference Question
    By smande3 in forum New To Java
    Replies: 1
    Last Post: 06-30-2012, 03:42 AM
  2. Replies: 5
    Last Post: 03-29-2012, 06:22 PM
  3. Using enhanced for loop to print array list??
    By bdl1127 in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 01:43 AM
  4. Array and Loop Problem
    By javadog in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 12:38 PM
  5. Adding Arrays and Enhanced For Loop into program.
    By vinyacam in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 06:28 AM

Tags for this Thread

Posting Permissions

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