用Java排序字符串数组 [英] Sorting string array in Java

查看:75
本文介绍了用Java排序字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教科书中有一个关于如何对字符串数组进行排序的示例,但是我很难理解代码的逻辑。我们有以下数组:

There is an example in my textbook for how to sort string arrays, but I am having a hard time understanding the logic of the code. We have the following array:

String[] words = {"so", "in", "very", "every", "do"};

方法本身如下:

public static void sortArray(Comparable[] compTab) {
    for (int next=1; next < compTab.length; next++) {
        Comparable value = compTab[next];
        int this;
        for (this = next; this > 0 && value.compareTo(compTab[this-1]) < 0; this--) {
            compTab[this] = compTab[this-1];
        }
        compTab[this] = value;
        writeArray(next + " run through: ", compTab);
    }
}

最后一个 writeArray 调用会导致打印以下文本,以便首次运行: 1。运行:每次都这样做

This last writeArray call results in the following text being printed for first run through: "1. run through: in so very every do"

确定。就像我说的那样,这段代码的逻辑有些问题。如果我们是第一次循环,这就是我看到的情况:

OK. Like I said, I have some problems with the logic in this code. If we go through the loop for the first time, this is what I see happening:


  1. 我们有:可比值= compTab [1]

我们以 this = next (其中== 1)。因此,Java将只经历一次内部循环。事实证明,对于此第一次运行, value.compareTo(compTab [this-1])的确小于0。因此我们具有: compTab [ 1] = compTab [0] 。这意味着以前位于[1]的单词现在被替换为[0]的单词。因此,我们现在在数组的位置[1]中有单词 so。

We start the inner loop with this = next (which == 1). Thus, Java will only go through the inner loop once. It turns out that for this first run value.compareTo(compTab[this-1]) is indeed less than 0. Thus we have: compTab[1] = compTab[0]. This means that the word that used to be in position [1] is now replaced with the word that used to be in position [0]. Thus, we now have the word "so" in position [1] of the array.

该方法的下一步是: compTab [this] =值。这就是我感到困惑的地方。这告诉我,由于此= 1,我们在这里得到 compTab [1] = value 。但是,在该方法的前面,我们定义了value = in。这告诉我数组中的位置[1]再次采用单词 in。

The next step in the method is: compTab[this] = value. This is where I get confused. This tells me that since this = 1, we here get compTab[1] = value. However, earlier in the method we defined value = "in". This tells me that position [1] in the array yet again assumes the word "in".

我认为,最终打印出来的应该是:

The way I see this, the final print out should then be:

1。贯穿:每做一次。

"1. run through: so in very every do".

换句话说,我遵循代码逻辑的方式,数组的最终打印结果与实现该方法之前的结果相同!显然,我的逻辑中有一部分是不正确的。例如-我看不到原来在位置[1]的单词现在在位置[0]的情况。如果有人可以帮助我解释一下,我将万分感谢!

In other words, the way I follow the logic of the code, the final print out of the array is just the same as it was before the method was implemented! Clearly there is some part of my logic here which is not correct. For instance - I don't see how the word that used to be in position [1] is now in position [0]. If anyone can help explain this to me, I would be extremely grateful!

推荐答案

public class A {

static  String Array[]={" Hello " , " This " , "is ", "Sorting ", "Example"};
String  temp; 


public static void main(String[] args)

{    

 for(int j=0; j<Array.length;j++)
 {
     for (int i=j+1 ; i<Array.length; i++)
     {
         if(Array[i].trim().compareToIgnoreCase(Array[j].trim())<0)
         {
             String temp= Array[j];
             Array[j]= Array[i]; 
             Array[i]=temp;


         }
     }

     System.out.print(Array[j]);
 }
}

}

这篇关于用Java排序字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆