James Gould
James Gould

Reputation: 4712

Struggling to understand how to loop over a list

I'm currently developing a POS till system as a university assignment and I'm completely stumped on a for loop that I need to implement to save any data that gets added to the system by the end user. The save() function is meant to save any data put into the program to 1 of 2 .txt files (stock or till).

I've currently built the save function for each of the different instance variables but when it runs through the save() method it'll only run once (obviously) and I'm having a hard time understanding how to implement a sample for loop as a solution.

Here's my current progress:

public void save() throws IOException {
    // IMPLEMENTATION
    PrintWriter outfileStock = new PrintWriter(new FileWriter(SHOP_STOCK_DATA_FILE));
    outfileStock.println(barcode);
    outfileStock.println(cost);
    outfileStock.println(quantity);

    outfileStock.close();

    PrintWriter outfileTill = new PrintWriter(new FileWriter(SHOP_TILL_DATA_FILE));
    outfileTill.println();
    outfileTill.println();
    outfileTill.println();

    outfileTill.close();
}

The sample for loop we've been given (from a worksheet that lead up to this assignment is this:

public void save(String fileName) throws IOException {
    PrintWriter outfile = new PrintWriter(new FileWriter(fileName));
    outfile.println(type);
    outfile.println(face);
    outfile.println(hair);
    outfile.println(powerPoints);
    outfile.println(loot.size());
    for (Treasure treasure : loot) {
        outfile.println(treasure.getName());
        outfile.println(treasure.getValue());
    }
    outfile.close();

While I'm not asking for the code to be written for me, it would be great if somebody to explain how the

for (Treasure treasure : loot) {
    outfile.println(treasure.getName());
    outfile.println(treasure.getValue());
}

loop works. I can provide some more info if needed, fairly new to Java so not sure how much is needed to understand.

Upvotes: 2

Views: 106

Answers (4)

adrCoder
adrCoder

Reputation: 3285

loot is an ArrayList that contains Treasure objects.

for (Treasure treasure : loot) {
    outfile.println(treasure.getName());
    outfile.println(treasure.getValue());
}

loops over each one of the Treasure objects (each one of which are temporarily assigned to treasure) by this line of code:

for (Treasure treasure : loot) {

which means (for every Treasure object in loot which you call treasure)

and gets (for each) their name (treasure.getName()) and their values (treasure.getValue()).

:

stands for the enhanced for-loop which was introduced in Java SE 5.0. See more info here:

https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with

Basically, instead of

for (int i=0; i < array.length; i++) {
    System.out.println("Element: " + array[i]);
}

you can now do

for (String element : array) {
    System.out.println("Element: " + element);
}

Upvotes: 1

Isuru Gunawardana
Isuru Gunawardana

Reputation: 2887

loot is a List. Therefore using this enhanced for loop it fetch the each element in each iteration and assign it to the treasure variable. But here you don't have access to the previous element of the list in a given time. If you use other for loop for(int x=0; x < size ; x++ ) in each iteration you can access previous or next element by coding `loot.get(x-1) or loot.get(x+1). Therefore it depends on your requirement.

Upvotes: 0

Joseph Larson
Joseph Larson

Reputation: 9078

This is basic Java. The variable loot is something that can be iterated over. It's either an array or one of the container classes like ArrayList. It contains Treasure objects.

For each element in the loot array, those two lines of code are executed.

Upvotes: 0

Serge Nassar
Serge Nassar

Reputation: 166

loot seems to be a List of some sort. What the for loop will do is take each element of this list and return them to you as a single object called treasure. when you get this element back you can treat is as a normal Treasure object. In your case it seems to be writing the treasure name and value to a file.

For each loop

Upvotes: 1

Related Questions