小妹刚接手java,希望大哥大姐多多帮忙!
我改写了个排列字符串长短的程序,就是按照字符串长短排列!
class Bubble2 { public static void main(String args[]) { String str[] = {"what","is","yours","departments" }; String q[]; int a, b; int size,result; size = 10; // number of elements to sort // display original array System.out.print("Original array is:"); for(int i=0; i < str.length; i++) System.out.print(" " + str[i]); System.out.println(); // This is the bubble sort. for(a=1; a < str.length; a++) for(b=str.length-1; b >= a; b--) { result=str[b-1].compareTo(str[b]); if(result>0) { // if out of order // exchange elements q[] = str[b-1]; str[b-1] = str[b]; str[b] = q[]; } } // display sorted array System.out.print("Sorted array is:"); for(int i=0; i < size; i++) System.out.print(" " + str[i]); System.out.println(); } }
采用冒泡法
q[] = str[b-1]; str[b-1] = str[b]; str[b] = q[]; 这里怎么交换两个字符串中的内容?
利用什么保存一个字符传》?