그래서 나는이 코드를 가지고 있고 그것을 인쇄 :{} 형식이 다릅니 까? 포맷 딜레마
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
루프에서 괄호를 생략하면
'{..}'는 코드 블록을 나타냅니다. 루프는 단일 명령 또는 단일 코드 블록을 실행할 수 있습니다. 따라서 루프의 끝 부분에'{'을 시작과'} '에 추가하지 않으면 첫 번째 명령 만 반복됩니다. 즉, for (...) foo(); bar();'for (...) {foo();와 동일합니다. } bar();'그래서'foo()'만 반복하지만'bar()'는 반복하지 않습니다. – Pshemo
중복 된 질문에 대한 첫 번째 대답은 여러분이 만든 질문과 정확히 같은 실수를 보여줍니다. (들여 쓰기가 잘못되어 코드가 보이지 않는 것처럼 보입니다) –