Topic: 请高手指教 关于一维数组

  Print this page

1.请高手指教 关于一维数组 Copy to clipboard
Posted by: flydog123456
Posted on: 2004-11-03 15:54

public class invertArray{
  public static void main(String args[]){
    int i,j;
String strArray[] = {"about","abandon","do","can","here","my","go"};
    int k = strArray.length;
    System.out.println("Resource string array is:");
    for(i = 0;i < k;i++)
    {
      System.out.print(strArray[i]+" ");      
    }
System.out.println("\n************************************");
    for(i = 0;i<k-1;i++)
    {
      for(j = i+1;j < k;j++)
      {
        if(strArray[i].compareTo(strArray[j])<0)
        {
          String tempStr = strArray[i];
          strArray[i] = strArray[j];
          strArray[j] = tempStr;
        }
      }
    }
    System.out.println("Result after sortting is:");
    for(i = 0;i < k;i++)
    {
      System.out.print(strArray[i]+" ");      
    }
  }
}

输出结果是:
resource string array is:
about abandon do can here my go
*****************************************
result after sorting is:
my here go do can about abandon

我不明白
"  for(i = 0;i<k-1;i++)
    {
      for(j = i+1;j < k;j++)
      {
        if(strArray[i].compareTo(strArray[j])<0)
        {
          String tempStr = strArray[i];
          strArray[i] = strArray[j];
          strArray[j] = tempStr;
        }
      }
    } "
这个语句是什么意思啊????是排序吗?是按什么顺序排的啊?
如果不是 那后面的输出的结果是什么意思????
希望高手指点啊 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(首先确定这个程序没有错!!)

2.Re:请高手指教 关于一维数组 [Re: flydog123456] Copy to clipboard
Posted by: why
Posted on: 2004-11-03 20:24

That's "bubble sort", sorted by
strArray[i].compareTo(strArray[j]) < 0
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#compareTo(java.lang.String)

my > here > go > do > can > about > abandon

3.Re:请高手指教 关于一维数组 [Re: flydog123456] Copy to clipboard
Posted by: kaiserlew
Posted on: 2004-11-03 20:52

按字典由大到小排序,第一个字母相同就比较第二个,直到比较出来为止

4.Re:请高手指教 关于一维数组 [Re: flydog123456] Copy to clipboard
Posted by: flydog123456
Posted on: 2004-11-03 22:37

哦 这样啊 明白了 谢谢大侠啊!!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923