1. 程式人生 > >java筆試題String字串單詞逆序how are you變成are you how

java筆試題String字串單詞逆序how are you變成are you how

今兒去面java,筆試上來六個程式設計大題也是醉了,兩頁紙全是英文也是棒棒噠。。。好多字串方面的,其實還是挺簡單的,但是有些方法感覺有點忘了。。。。。。總之看面試官看我程式碼的表情我也是醉了。。。。

大概題意就是寫個函式reverse words in String,how are you變成are you how這種,懶的寫就跟他說了說思路什麼分割什麼反轉,回來之後自己又編了一下發現真的挺簡單的。。。面試官說什麼字串長度不固定,我居然還說用什麼ArrayList。。。額其實好像只用String陣列就可以啊?

感覺自己圖樣圖森破。。。。。六個題準備都整理上來,這回又掛了估計微笑

class Test2 {
	public static void converse(String s) {
		String[] ss = s.split(" ");
		String temp = "";
		int bb = ss.length;
		for (int i = 0; i < bb/2; i++) {
			temp = ss[i];
			ss[i] = ss[bb-1-i];
			ss[bb-1-i] = temp;
		}
		for(int x=0;x<bb;x++){
			System.out.println(ss[x]);
		}
	}

	public static void main(String[] args) {
		String str = "how are you hehe";
 		Test2.converse(str);
		
	}
}
輸出

hehe
you
are
how