在 Java 中将排序数字字符串添加到 SortedSet 时出现问题

发布于 2024-11-10 11:36:09 字数 1781 浏览 5 评论 0原文

我创建了一个自己的 SortedSet,这里是向数组添加内容的代码。 (我知道有比数组更好、更简单的方法来做到这一点,但它必须以这种方式完成)

public boolean add(AnyType x){
    if(this.contains(x))
        return false;
    else if(this.isEmpty()){
        items[theSize]=x;
        theSize++;
        return true;
    }
    else{
    if( theSize == items.length )
        this.grow();
    //Here goes code for adding
    theSize++;
    AnyType[] newItems = (AnyType[]) new Comparable[theSize];
    for(int i=0;i<theSize-1;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+2;j<theSize;j++)
                newItems[j]=items[j-1];
            items = newItems;
            return true;
        }
        else
            newItems[i]=items[i];
    newItems[theSize-1] =x;
    items=newItems;
    return true; //*/
    }
}

我正在测试这样的排序数字字符串:

public static void main(String[] a) {


    SortedSet<String> s1 = new SortedSet<String>();
    final int NUM1 = 37;
    final int NUM2 = 53;
    final int NUM3 = 61;

    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM2) {
        s1.remove("" + i);
    }

    s1.show();

    }

在输出中我得到:

1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 4 7 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 7 8 9

我的问题是如何使其按应有的方式排序?我知道问题出在添加方法中(它也应该能够对字符串和整数进行排序)

并且当我创建字符串或整数的 SortedSet 时它工作得很好,当我像这样混合它们时我得到这个“未排序”结果。

帮助?? 谢谢。

I created a my own SortedSet here is the code for adding something to the array. (I know there are better and simpler ways to do this than an Array but it has to be done this way)

public boolean add(AnyType x){
    if(this.contains(x))
        return false;
    else if(this.isEmpty()){
        items[theSize]=x;
        theSize++;
        return true;
    }
    else{
    if( theSize == items.length )
        this.grow();
    //Here goes code for adding
    theSize++;
    AnyType[] newItems = (AnyType[]) new Comparable[theSize];
    for(int i=0;i<theSize-1;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+2;j<theSize;j++)
                newItems[j]=items[j-1];
            items = newItems;
            return true;
        }
        else
            newItems[i]=items[i];
    newItems[theSize-1] =x;
    items=newItems;
    return true; //*/
    }
}

And I am testing sorted number strings like this:

public static void main(String[] a) {


    SortedSet<String> s1 = new SortedSet<String>();
    final int NUM1 = 37;
    final int NUM2 = 53;
    final int NUM3 = 61;

    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM3) {
        s1.add("" + i);
    }
    s1.show();
    for (int i = NUM1; i != 0; i = (i + NUM1) % NUM2) {
        s1.remove("" + i);
    }

    s1.show();

    }

And in the output I get:

1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 33 34 35 36 37 38 39 4 40 41 42 43 44 45 46 47 48 49 5 50 51 52 53 54 55 56 57 58 59 6 60 7 8 9

My question is how do I make this to be sorted the way it should be?? I know the problem is in the adding method (it should be able to sort strings and integers as well)

And it works perfectly fine when I create a SortedSet of Strings or Integers, when when I mix them like this I get this "unsorted" outcome.

Help??
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-11-17 11:36:09

对我来说,这些看起来像是排序好的字符串。 “1”位于“10”之前,就像字典中“a”位于“ab”之前一样。如果您希望它们按数字顺序排序,@MRAB 有正确的建议将表示数字的字符串转换为实际数字。

如果您想将您的集合保留为 SortedSet (在下面的代码片段中未执行错误检查),您可以使用 Comparator 来做到这一点:

    SortedSet<String> s1 = new TreeSet<String>(new Comparator<String>() {
        /**
         * Returns a positive value if number1 is larger than number 2, a
         * negative number if number1 is less than number2, and 0 if they
         * are equal.
         */
        public int compare(String number1, String number2) {
            return Integer.parseInt(number1) - Integer.parseInt(number2);
        }
    });

Those look like sorted strings to me. "1" comes before "10" just like "a" comes before "ab" in the dictionary. @MRAB has the correct suggestion to convert your strings representing numbers to actual numbers if you want them sorted in numerical order.

You can do that with a Comparator if you want to keep your set a SortedSet<String> (error checking not performed in the snippet below):

    SortedSet<String> s1 = new TreeSet<String>(new Comparator<String>() {
        /**
         * Returns a positive value if number1 is larger than number 2, a
         * negative number if number1 is less than number2, and 0 if they
         * are equal.
         */
        public int compare(String number1, String number2) {
            return Integer.parseInt(number1) - Integer.parseInt(number2);
        }
    });
少女七分熟 2024-11-17 11:36:09

在比较之前将数字字符串转换为数字。

或者,当比较两个不同长度的数字字符串时,通过在开头添加零来将较短的数字字符串填充为与较长的数字字符串相同的长度。

Convert the number strings to numbers before comparing them.

Alternatively, when comparing two number strings which are different lengths, pad the shorter one to be the same length as the longer one by adding zeros to the start.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文