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.