新手java中怎麼將一個字符數組反序,不要new數組,只能使用一個字符數組,三克油
可以用Collection.reverse(list)呀,先把你的數組放到List裡,再Collection.reverse(list),然後再從list中拿回來,示例代碼:
public static void main(String[] args) {
String[] deal = new String[3];
deal[0] = "0";
deal[1] = "1";
deal[2] = "2";
List<String> list = new ArrayList<String>();
for (int i = 0; i < deal.length; i++) {
list.add(i, deal[i]);
}
Collections.reverse(list);
for (int i = 0; i < list.size(); i++) {
deal[i] = list.get(i);
}
for (int i = 0; i < deal.length; i++) {
System.out.println(deal[i]);
}
}