0

So I have this code and I print it:

import java.util.*;

public class Reverse_Copy {

    public static void main(String[] args){

        //create an array and covert to list
        Character[] ray = {'p','w','n'};
        List<Character> l = new ArrayList<Character>(Arrays.asList(ray));
        System.out.println("List is: ");
        output(l);

        //reverse and print out the list
        Collections.reverse(l);
        System.out.println("After reverse: ");
        output(l);

        //create a new array and a new list
        Character[] newray = new Character[3];
        List<Character> listCopy = new ArrayList<Character>(Arrays.asList(newray));

        //copy contens of list l into list listCopy
        Collections.copy(listCopy, l);
        System.out.println("Copy of list: ");
        output(listCopy);

        //fill collection with crap
        Collections.fill(l,'X');
        System.out.println("After filling the list: ");
        output(l);

    }

    private static void output(List<Character> thelist){

        for(Character thing: thelist)
            System.out.printf("%s ", thing);
            System.out.println();


    }

}

This is the print:

List is: 
p w n 
After reverse: 
n w p 
Copy of list: 
n w p 

This is the same code with brackets for the enhanced for loops.

    import java.util.*;

    public class Reverse_Copy {

    public static void main(String[] args){

        //create an array and covert to list
        Character[] ray = {'p','w','n'};
        List<Character> l = new ArrayList<Character>(Arrays.asList(ray));
        System.out.println("List is: ");
        output(l);

        //reverse and print out the list
        Collections.reverse(l);
        System.out.println("After reverse: ");
        output(l);

        //create a new array and a new list
        Character[] newray = new Character[3];
        List<Character> listCopy = new ArrayList<Character>(Arrays.asList(newray));

        //copy contens of list l into list listCopy
        Collections.copy(listCopy, l);
        System.out.println("Copy of list: ");
        output(listCopy);

        //fill collection with crap
        Collections.fill(l,'X');
        System.out.println("After filling the list: ");
        output(l);

    }

    private static void output(List<Character> thelist){

        for(Character thing: thelist){
            System.out.printf("%s ", thing);
            System.out.println();
        }

    }

}

Here is the print:

List is: 
p 
w 
n 
After reverse: 
n 
w 
p 
Copy of list: 
n 
w 
p 
After filling the list: 
X 
X 
X 

I find this very peculiar, what do the simple brackets have anything to do with the way something is printed? Any thoughts on why this is happening? You can test it out yourself if you don't believe me. I cannot find any reasoning for this to happen. Very Weird.


EDIT: The answers in the duplicate website only prove how it doesn't change anything. Proof of how adding curly braces changes things rather than keeping it same.

  • `{..}` represents code block. Loops can run either single instruction, or single code block. So if you will not add `{` at start and `}` at end of code which should be looped only first instruction will be looped. In other words `for (...) foo(); bar();` is same as `for (...){ foo(); } bar();` so it will loop only `foo()` but not `bar()`. – Pshemo Jan 20 '15 at 02:26
  • 1
    The very first answer on the duplicate question shows exactly the same mistake as the one that you are making (your indenting is bad which makes your code look like something that it isn't) – Erwin Bolwidt Jan 20 '15 at 02:43

1 Answers1

1

When you omit the brackets in your for loop, only the first line executes:

 for(Character thing: thelist)
        System.out.printf("%s ", thing);
        System.out.println();

Which will execute the printf line for each element in thelist and THEN execute the println only once. Don't let the indenting fool you.

And

 for(Character thing: thelist){
    System.out.printf("%s ", thing);
    System.out.println();
}

Which will execute the printf line and the println line for each element in thelist.

Todd
  • 30,472
  • 11
  • 81
  • 89
  • Thank You, very peculiar though shouldn't it not do that. Then having curly braces rather seems pointless. –  Jan 20 '15 at 02:27
  • Interesting. I enforce the "must always have curly braces, even for a single statement" rule on my team because it leads to odd errors otherwise. But I see your point in this example. – Todd Jan 20 '15 at 02:29
  • 1
    @Asker123 Curly braces helps us maintain code. For instance if you have code like `for(...){code();}` and you want to add new lines at the end of loop you can easily see if added code will be executed by loop (by adding it before `}`) or not (if we added it after `}`). In case of `for(...)code(); it is easy to forget about need to surround more than one instructions with `{` `}` because we are under impression that indentation defines scope of the loop (like in code from your question), while in reality `{..}` defines scope, but indentation just helps us see it. – Pshemo Jan 20 '15 at 02:36