public class ReverseString {
Stack word;
Stack sentence;
public void reverse(String sentence){
for(int i=0; i<sentence.length(); i++){
char n =sentence.charAt(i);
if(n!=' '){
word.push(n);
}else{
while(!word.empty()){
this.sentence.push(word.pop());
}
this.sentence.push(' ');
}
}
}
public void show(){
while(!this.sentence.empty()){
System.out.println(this.sentence.pop());
}
}
public static void main(String[] args) {
new ReverseString().reverse("the sky is blue");
}
}
想不明白为何在执行word.push(n)时 会报 java.lang.NullPointerException