2015-02-10 152 views
0

这是给予代码:数组排序的字母排序

// bubbleSort.java 
// demonstrates bubble sort 
// to run this program: C>java BubbleSortApp 
//////////////////////////////////////////////////////////////// 
class ArrayBub 
    { 
    private long[] a;     // ref to array a 
    private int nElems;    // number of data items 
//-------------------------------------------------------------- 
    public ArrayBub(int max)   // constructor 
     { 
     a = new long[max];     // create the array 
     nElems = 0;      // no items yet 
     } 
//-------------------------------------------------------------- 
    public void insert(long value) // put element into array 
    { 
    a[nElems] = value;    // insert it 
    nElems++;      // increment size 
    } 
//-------------------------------------------------------------- 
    public void display()    // displays array contents 
    { 
    for(int j=0; j<nElems; j++)  // for each element, 
    System.out.print(a[j] + " "); // display it 
    System.out.println(""); 
    } 
//-------------------------------------------------------------- 
    public void bubbleSort() 
    { 
    int out, in; 

     for(out=nElems-1; out>1; out--) // outer loop (backward) 
    for(in=0; in<out; in++)  // inner loop (forward) 
     if(a[in] > a[in+1])  // out of order? 
      swap(in, in+1);   // swap them 
     } // end bubbleSort() 
//-------------------------------------------------------------- 
    private void swap(int one, int two) 
    { 
    long temp = a[one]; 
    a[one] = a[two]; 
    a[two] = temp; 
    } 
//-------------------------------------------------------------- 
    } // end class ArrayBub 
//////////////////////////////////////////////////////////////// 
class BubbleSortApp 
    { 
    public static void main(String[] args) 
    { 
    int maxSize = 100;   // array size 
    ArrayBub arr;     // reference to array 
    arr = new ArrayBub(maxSize); // create the array 

    arr.insert(77);    // insert 10 items 
    arr.insert(99); 
    arr.insert(44); 
    arr.insert(55); 
    arr.insert(22); 
    arr.insert(88); 
    arr.insert(11); 
    arr.insert(00); 
    arr.insert(66); 
    arr.insert(33); 

    arr.display();    // display items 

    arr.bubbleSort();    // bubble sort them 

    arr.display();    // display them again 
    } // end main() 
    } // end class BubbleSortApp 
//////////////////////////// 

我需要改变这个计划,使其插入字符串和按字母顺序显示出来。这是我到目前为止:

class ArrayBub 
    { 
    private String[] a;     // ref to array a 
    private String nElems;    // number of data items 
//-------------------------------------------------------------- 
    public ArrayBub(int max)   // constructor 
    { 
    a = new String[max];     // create the array 
    nElems = "";      // no items yet 
    } 
    //-------------------------------------------------------------- 
     public void insert(String value) // put element into array 
    { 
    a[nElems] = value;    // insert it 
    nElems++;      // increment size 
    } 
//-------------------------------------------------------------- 
    public void display()    // displays array contents 
    { 
    for(int j=0; j<nElems; j++)  // for each element, 
    System.out.print(a[j] + " "); // display it 
    System.out.println(""); 
    } 
//-------------------------------------------------------------- 
    public void bubbleSort() 
    { 
    String out; 
    String in; 

    for(out=nElems-1; out>1; out--) // outer loop (backward) 
    for(in=0; in<out; in++)  // inner loop (forward) 
     if(a[in] > a[in+1])  // out of order? 
      swap(in, in+1);   // swap them 
    } // end bubbleSort() 
//-------------------------------------------------------------- 
    private void swap(String one, String two) 
    { 
    String temp = a[one]; 
    a[one] = a[two]; 
    a[two] = temp; 
    } 
//-------------------------------------------------------------- 
    } // end class ArrayBub 
    //////////////////////////////////////////////////////////////// 
class BubbleSortApp 
{ 
    public static void main(String[] args) 
    { 
    int maxSize = 100;   // array size 
    ArrayBub arr;     // reference to array 
    arr = new ArrayBub(maxSize); // create the array 

    arr.insert("hello"); 
    arr.insert("this"); 
    arr.insert("is"); 
    arr.insert("a"); 
    arr.insert("random"); 
    arr.insert("weird "); 
    arr.insert("sentence"); 
    arr.insert("that"); 
    arr.insert("does"); 
    arr.insert("not"); 
    arr.insert("make"); 
    arr.insert("any"); 
    arr.insert("sense"); 

    arr.display();    // display items 

    arr.bubbleSort();    // bubble sort them 

    arr.display();    // display them again 
} // end main() 
} // end class BubbleSortApp 

我有很多错误。我很难从变量到字符串改变正确的功能。我明白一旦我在代码中的一个点,但我不能使用其他功能等等

+1

如果你有一个特定的问题,请询问。但是一般的“修复我的代码中的所有错误”在SO – Ascalonian 2015-02-10 18:45:25

+4

上不起作用,而输入大写字母将无济于事。 – 2015-02-10 18:46:11

+0

@Ascalonian - 我同意。托马斯,你把nElems改成了一个字符串类型,但是你的评论直接在后面说它意味着包含一个数字。这种低级别的误解使得很难知道我们甚至可以帮助您尽可能少地为您重写所有代码。 – lukevp 2015-02-10 18:47:08

回答

3
Arrays.sort(a); 

这将会把数组“A”字母顺序