2015-01-20 4 views
0

그래서 나는이 코드를 가지고 있고 그것을 인쇄 :{} 형식이 다릅니 까? 포맷 딜레마

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

이는 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(); 


    } 

} 

이 인쇄입니다.

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

가 나는 이것이 매우 독특한 찾아 방식 뭔가 아무 상관이 간단한 괄호를 인쇄 할 것 : 여기

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(); } } } 

인쇄입니까? 왜 이런 일이 일어나고 있는지에 대한 생각? 당신이 나를 믿지 않으면 너 자신을 시험해 볼 수있다. 나는이 일이 일어날 이유를 찾을 수 없다. 엄청 이상해.


편집 : 중복 된 웹 사이트의 대답은 단지 그것을 아무것도 변경하지 않는 방법을 증명한다. 중괄호를 추가하는 것이 일들을 동일하게 유지하는 대신 변경하는 방법을 증명합니다. 당신이 당신의 for 루프에서 괄호를 생략하면

+0

'{..}'는 코드 블록을 나타냅니다. 루프는 단일 명령 또는 단일 코드 블록을 실행할 수 있습니다. 따라서 루프의 끝 부분에'{'을 시작과'} '에 추가하지 않으면 첫 번째 명령 만 반복됩니다. 즉, for (...) foo(); bar();'for (...) {foo();와 동일합니다. } bar();'그래서'foo()'만 반복하지만'bar()'는 반복하지 않습니다. – Pshemo

+1

중복 된 질문에 대한 첫 번째 대답은 여러분이 만든 질문과 정확히 같은 실수를 보여줍니다. (들여 쓰기가 잘못되어 코드가 보이지 않는 것처럼 보입니다) –

답변

1

, 첫 번째 행이 실행 : thelist의 각 요소에 대한 printf 줄을 실행 한 후 한 번만 println를 실행합니다

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

. 들여 쓰기가 당신을 속일 수 없게하십시오. printf 라인 thelist의 각 요소에 대한 println 라인을 실행할

그리고

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

.

+0

고맙습니다. 그러지 마. 그런 다음 중괄호를 사용하는 것이 무의미한 것처럼 보입니다. –

+0

흥미 롭습니다. 나는 "항상 중괄호를 사용해야한다. 심지어 단일 문장 일지라도"규칙을 지키도록한다. 그렇지 않으면 이상한 오류가 발생하기 때문이다. 그러나 나는이 예에서 당신의 요지를 본다. – Todd

+1

@ Asker123 중괄호는 코드를 유지 관리하는 데 도움이됩니다. 예를 들어'for (...) {code();}'과 같은 코드가 있고 루프 끝 부분에 새 행을 추가하려는 경우 추가 된 코드가 루프에 의해 실행되는지 쉽게 볼 수 있습니다 (전에 '}') 그렇지 않으면 (우리가'}'다음에 추가 한 경우). 'for (...) code(); 들여 쓰기가 루프의 범위를 정의하고 있다는 느낌이 들기 때문에 ('''')''와 함께 하나 이상의 명령어를 둘러 쌀 필요성을 잊기 쉽습니다. 실제로는 {..}' 범위를 정의하지만 들여 쓰기는 우리가 그것을 볼 수 있도록 도와줍니다. – Pshemo

관련 문제