Key Ideas
[Part 3 of 11]


There are eight key ideas in this part:

  1. To construct a standard for loop, use, for example:

    for (int i = 0; i < 10; i++) {
       // do stuff, sometimes using i,
       // but usually not changing i
    }

  2. You can loop through an array by using the loop index and the array index:

    for (int i = 0; i < intArray.length; i++) {
       intArray[i] = i * 10;
       System.out.println(intArray[i]);
    }

    It is sensible to use the array.length in the condition.
  3. There is nothing to stop you using the same index to loop through multiple arrays if the same size (or the non-condition one is larger):

    for (int i = 0; i < intArrayA.length; i++) {
       intArrayA[i] = intArrayB[i] * 10;
       System.out.println(intArrayA[i]);
    }

  4. You can nest loops inside each other, in which case the inner loop is run to completion each time the outer loop increments:

    for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 100; j++) {
          System.out.println(i + " x " + j + " = " + i*j);
       }
    }

  5. This is especially useful when looping through 2D arrays:

    for (int i = 0; i < intArray.length; i++) {
       for (int j = 0; j < intArray[i].length; j++) {
          intArray[i][j] = 10;
          System.out.println(intArray[i][j]);
       }
    }

  6. To construct a standard if-else statement, use, for example:

    if (i == 10)
       // do stuff
    } else {
       // do something else
    }

  7. If you have multiple conditions, and you know the probabilities associated with your conditions, use an if/else/if ladder, putting the most probable highest:

    if (i == 10) {
       // do stuff
    } else if (i == 9) {
       // do stuff
    } else if (i == 8) {
       // do stuff
    } else {
       // do stuff
    }

  8. If you aren't sure of the probabilities and you have more than ~three options, use a switch statement:

    String greeting = "Hello";

    switch (greeting) {
       case "Hello":
          System.out.println("Hello");
          break;
       case "Hi":
          System.out.println("Hi");
          break;
       default:
          System.out.println("Bonjour, mes ami");
    }


Here's some code which uses these ideas:

int [] intArray = new int[10];
for (int i = 0; i < intArray.length; i++) {
   intArray[i] = i;
}

int[] tempArray = new int[intArray.length];
for (int i = 0; i < intArray.length; i++) {
   tempArray[i] = intArray[i];
}

intArray = null;
intArray = new int[tempArray.length - 1];
int deletePosition = 6;

for (int i = 0; i < deletePosition; i++) {
   intArray[i] = tempArray[i];
}

for (int i = deletePosition; i < tempArray.length; i++) {
   intArray[i - 1] = tempArray[i];
}

for (int i = 0; i < intArray.length; i++) {
   System.out.println(intArray[i]);
}

And some more: what do you think the difference between print and println is? What do you think this code does? Can you think of a way to test it on a small array?

for (int i = 0; i < intArray.length; i++) {
   for (int j = 0; j < intArray[i].length; j++) {
      System.out.print(intArray[i][j] + " ");
   }
   System.out.println("");
}

Here's some code which uses these ideas to shift an array down a row, avoiding the boundary problem:

for (int i = 0; i < arrayA.length; i++) {

   for (int j = 0; j < arrayA[i].length; j++) {

      if (i != 0) {
         arrayB[i][j] = arrayA[i - 1][j];
      } else {
         arrayB[i][j] = arrayA[arrayA.length - 1][j];

      }

   }

}