将项目添加到有序集中的数组时出现问题

发布于 2024-11-10 09:16:34 字数 1394 浏览 6 评论 0原文

我在向数组集中添加项目时遇到问题,它应该按升序排序,由于某种原因,我只是添加它,但它没有排序。

这是我的代码:

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
    /*AnyType[] newItems = (AnyType[]) new Comparable[items.length];
    newItems = items;
    for(int i=0;i<theSize;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+1;j<theSize;j++)
                newItems[j]=items[i];
            items = newItems;
            theSize++;
            return true;
        }

    //*/
    items[theSize]=x; //*/
    theSize++;
    return true;
    }
}

该方法不应允许重复某个项目,因此如果您尝试添加其中已有的内容,它应该返回 false。如果数组为空,只需添加到 items[0] ,然后我尝试创建一个新数组,一旦找到比我输入的项目更大的项目,请将所有内容复制到新数组中,添加新值,然后添加其余的然后 make items = newItems; 但它不起作用。我已经尝试了几个小时,所以我决定寻求帮助。 我的 SortedSet 类定义如下:

public class SortedSet<AnyType extends Comparable> implements Set<AnyType>
{
    private AnyType[] items;
    private int theSize;
    public SortedSet(){
        theSize = 0;
        items = (AnyType[]) new Comparable[5];
    }

我知道还有其他方法可以做到这一点,例如使用 TreeMap,但它必须作为数组来完成。

谢谢

I am having trouble adding an item to my array set, it should be ordered in ascending order and for some reason I just get to add it and it does not order it.

Here is my code:

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
    /*AnyType[] newItems = (AnyType[]) new Comparable[items.length];
    newItems = items;
    for(int i=0;i<theSize;i++)
        if(items[i].compareTo(x)>0){
            newItems[i]=x;
            newItems[i+1]=items[i];
            for(int j=i+1;j<theSize;j++)
                newItems[j]=items[i];
            items = newItems;
            theSize++;
            return true;
        }

    //*/
    items[theSize]=x; //*/
    theSize++;
    return true;
    }
}

The method should not allow an item to repeat so if you try to add something that is already in there, it should return false. If the array is empty just add to items[0] and then I tried to create a new array and once you find an item bigger than the one I'm inputing copy everything into a new array, add the new value, and just add the rest and then make items = newItems; but it did not work. I have been trying for a couple hours now so I just decided to ask for help.
I have my SortedSet class defined like this:

public class SortedSet<AnyType extends Comparable> implements Set<AnyType>
{
    private AnyType[] items;
    private int theSize;
    public SortedSet(){
        theSize = 0;
        items = (AnyType[]) new Comparable[5];
    }

I know there are other ways to do this like using TreeMap but it has to be done as an array.

Thanks

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

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

发布评论

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

评论(3

微暖i 2024-11-17 09:16:34

我不明白你在哪里写了以前的值?

在最后一个 else 中尝试类似的事情。

            theSize++;
            AnyType[] newItems = new AnyType[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;

由于我已经对其进行了编码以确保其正常工作,因此这里是一个处理字符串的示例。我在这里只关注正确的添加。对OP来说重要的其他问题被忽略了。

import java.util.Arrays;

public class OrderingAddTest
{
    static int theSize = 0;
    static String[] items = new String[theSize];
    public static void main(String[] args)
    {
        add("a");
        add("e");
        add("d");
        add("b");
        add("c");

        System.out.println(Arrays.toString(items)+"\n; items.length="+items.length);
    }

    private static boolean add(String x)
    {
        theSize++;
        String[] newItems = new String[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;
    }
}

I do not see where you are writing the previous values?

Try something like this in the last else.

            theSize++;
            AnyType[] newItems = new AnyType[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;

Since I already coded it to make sure it works fine, here is a sample working on Strings. I focused here only on correct adding. The other issues important for the OP were omitted.

import java.util.Arrays;

public class OrderingAddTest
{
    static int theSize = 0;
    static String[] items = new String[theSize];
    public static void main(String[] args)
    {
        add("a");
        add("e");
        add("d");
        add("b");
        add("c");

        System.out.println(Arrays.toString(items)+"\n; items.length="+items.length);
    }

    private static boolean add(String x)
    {
        theSize++;
        String[] newItems = new String[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;
    }
}
仅此而已 2024-11-17 09:16:34

已经有一个具有这些功能的本机 Java 类:java.util.TreeSet。

Set 中,同一个元素不能出现两次。 “add”方法返回一个布尔值:如果添加了该项目,则返回 true;如果该项目已在集合中,则返回 false。

TreeSet 是“SortedSet”的实现,它维护元素的顺序。您的商品只需实施“Comparable”即可。

There is already a native Java class that have theses features: java.util.TreeSet.

In a Set, the same element cannot appear twice. The "add" method returns a boolean: true if the item was added, false if it was already in the set.

TreeSet is an implementation of "SortedSet", that maintain an order on the elements. Your items only have to implement "Comparable".

意中人 2024-11-17 09:16:34

你的物品与物品的大小相同,应该是 Size+1

我猜你在这里遇到了问题

for(int j=i+ 1; j < theSize;j++)

newItems[j]=items[i];

这里 j 肯定会递增,但是没有为 i 给出递增操作,因此此循环中的所有新项目都被分配相同的值


编辑::

    int i;

    for(i=0; i < theSize+1 ; i++)
    if(items[i].compareTo(x)>0){
    newItems[i]=x;

    for(;i<theSize + 1; i++)
     newItems[i]=items[i-1];

执行下一步。

编辑2:

你在这里也做错了事,

items = newItems;

和之前的赋值语句(newItems=items)

你不能将它们等同,因为它们的大小不同,如果你使用指针,你可以做到这一点,(这里使用指针将使这个任务变得更容易,)

Your items is of the same size as of items, it should be Size+1

I guess you got a problem here

for(int j=i+ 1; j < theSize;j++)

newItems[j]=items[i];

here j is incrementing for sure, but there is no increment operation given for i, so all the newitems in this loop are being assigned the same value


EDIT :

    int i;

    for(i=0; i < theSize+1 ; i++)
    if(items[i].compareTo(x)>0){
    newItems[i]=x;

    for(;i<theSize + 1; i++)
     newItems[i]=items[i-1];

: do whatever next.

EDIT 2 :

U are doing a wrong thing here also,

items = newItems;

and the previous assigning statement (newItems=items)

you can't equate them as they are not of same size, you can do that if you use Pointer, (Using Pointers here will make this task A LOT easier,)

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