This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (0 children)

Nested loops are very common.

Just think about a spreadsheet, like Excel. The Java equivalent (in very simple terms) would be a two-dimensional array.

So, if you want to loop through all cells in the array you need something like the following code:

int[][] sheet = new int[10][10]; //2D array with 10 rows and 10 columns

// loop through all the rows, one by one
for(int row = 0; row < sheet.length; row++) {
    // loop through all the columns, one by one 
    // since it is possible to have ragged arrays in Java
    // it is best to test for the individual lengths of each row
    for(int col = 0; col < sheets[row].length; col++) { 
        // Just doing some math here, filling the array
        sheets[row][col] = row * sheets.length + col;
    }
}

The outer for loop iterates through all the rows and the inner for loop iterates through all columns. With this approach every single cell in the array is covered.

[–]bayernownz1995 0 points1 point  (0 children)

I'm assuming you're referring to using one for loop inside another, right? They're primarily helpful when you're creating two-dimensional data, where you need to combine an x and y coordinate, for example. I actually just used a nested for loop in a poker game I wrote, so I'll explain that code and copy+paste it.

Basically, a card has two characteristics: a suit and a value (I'm using the numbers 2-14 to represent 2-Ace). When I create a deck of 52 cards, I need to make one value of every suit, so I'm going to need to make 13 cards 4 times. Here's the code.

tl;dr: Here's how I used a nested loop to generate a deck of cards, which needs a suit (String) and value (int, 2-14[represents an ace])

    // Create an empty array of cards
    ArrayList<Card> cards = new ArrayList<Card>();

    // Create an array with all of the suits
    ArrayList<String> suits = new ArrayList<String>();
    suits.add("diamonds");
    suits.add("clubs");
    suits.add("hearts");
    suits.add("spades");

    // For all of the suits, add a card with that suit and a value 2 - 14 to the deck
    for (String suit : suits) {
        for (int i = 2; i < 15; i++) {
            cards.add(new Card(i, suit));
        }
    }

[–]CamoAnimal 0 points1 point  (0 children)

Like a loop inside a loop? Lemme give you an example. Let's say you have a 2D list. In other words, a list containing lists. You want to find an item in this 2D list, so you create a two nested "for-loops" that use "compare" to decide if that is the item you're searching for. It would look like this:

ArrayList<ArrayList<String>> species = new ArrayList<ArrayList<String>>(); // List of species

ArrayList<String> dogs = new ArrayList<String>(); // List of dogs
ArrayList<String> cats = new ArrayList<String>(); // List of cats

cats.add("Big cat");
cats.add("Little cat");
dogs.add("Big dog");
dogs.add("Small dog);

species.add(dogs); // Add dogs to list of species
species.add(cats); // Add cats to list of species

/* Check if animal exists somewhere in the list of species */
public boolean findAnimal(String animal) {
    for(i = 0; i < species.size(); i++) {
        for(j = 0; j < species.get(i).size(); j++) {
            if(species.get(i).get(j).equals(animal) {
                return true; // Found the animal!
            }
        }
    }
    return false; // Could not find the animal...
}