Java 移动数组列表

Java 移动数组列表,java,arraylist,Java,Arraylist,我已经编写了一个SortedIntList类,它有一个add-and-get方法 我调用以下四种方法: SortedIntList mySortedIntList = new SortedIntList(); mySortedIntList.add(9); mySortedIntList.add(7); System.out.println("0 is :"+mySortedIntList.get(0)); System.out.println("1 is :"+mySortedIntList.

我已经编写了一个SortedIntList类,它有一个add-and-get方法

我调用以下四种方法:

SortedIntList mySortedIntList = new SortedIntList();
mySortedIntList.add(9);
mySortedIntList.add(7);
System.out.println("0 is :"+mySortedIntList.get(0));
System.out.println("1 is :"+mySortedIntList.get(1));
我的get和add方法如下所示:

public void add(Integer newValue) {
    int position = 0;
    while(position < list.size()){
        int currentPosValue = list.get(position);
        if(newValue <= currentPosValue){
            for(int i=list.size()-1; i>=position; i--){
                int toBeShifted = list.get(i);
                list.set(i+1, toBeShifted);
            }
            list.set(position, newValue);
            return;
        }
        position++;
    }
    list.add(newValue);
}


       public int get(int i) throws IndexOutOfBoundsException {
    // Postcondition: If i < 0 or i >= size() throws 
    // IndexOutOfBoundsException, otherwise returns the value 
    // at position i of this IntList
    if (i < 0 || i >= list.size()) {
        throw new IndexOutOfBoundsException("SortedIntList.get");
    } else {
        return ((Integer) list.get(i)).intValue();
    }
}





    public int get(int i) throws IndexOutOfBoundsException {
        // Postcondition: If i < 0 or i >= size() throws 
        // IndexOutOfBoundsException, otherwise returns the value 
        // at position i of this IntList
        if (i < 0 || i >= list.size()) {
            throw new IndexOutOfBoundsException("SortedIntList.get");
        } else {
            return ((Integer) list.get(i)).intValue();
        }
    }
public void add(整数newValue){
int位置=0;
while(位置=size()抛出
//IndexOutOfBoundsException,否则返回值
//在该列表的位置i处
如果(i<0 | | i>=list.size()){
抛出新的IndexOutofBoundSexceception(“SortedIntList.get”);
}否则{
return((整数)list.get(i)).intValue();
}
}
public int get(int i)抛出IndexOutOfBoundsException{
//后置条件:如果i<0或i>=size()抛出
//IndexOutOfBoundsException,否则返回值
//在该列表的位置i处
如果(i<0 | | i>=list.size()){
抛出新的IndexOutofBoundSexceception(“SortedIntList.get”);
}否则{
return((整数)list.get(i)).intValue();
}
}
我已经在纸上写了出来,这似乎是合乎逻辑的,但代码却在:


System.out.println(“1是:“+mySortedIntList.get(1))
行,显然1是边界外的,但我不知道如何使用。

使用这个标准Java方法可能更容易,它将为您的集合排序。这样你们就不用自己处理分类了,祝你们好运

我发现了几个问题

首先,
list.set(i+i,toBeShifted)可能应该是
list.set(i+1,toBeShifted)。将7添加到列表时,列表大小为1。在for循环中,将i初始化为0(列表大小为-1)。当您调用list.set(i+i,toBeShifted)时,您正在调用list.set(0,toBeShifted),因此不会实际移动值


第二,虽然你不会在加9和7的时候遇到它,但是你会在一个无限的while循环中结束。你永远不会改变位置的值。如果你加上9,然后再加上一个更大的数字,你就完了。

阅读Java文档会有所帮助。显然,使用set()需要在您试图覆盖的位置已经有一个值。我需要使用add(position,value)来代替:-)

您不能使用list的
set()
来添加到列表中:例如,如果您尝试将索引1中的某个内容设置为大小为1的列表中的某个内容,您将得到一个
IndexOutOfBoundsException


基本上,你需要先添加

显然,我添加的代码标签不正确,我如何解决这个问题?你会想看看FAQ,因为你没有在这个论坛上使用[code][/code]标签。相反,您按下{}code按钮,代码4空格缩进。您发布的是
add
方法,而不是
get
。列表是什么?是ArrayList吗?您可以发布初始化代码吗?我强烈建议您使用调试器运行代码,这样您就可以在代码的每一点上看到列表中发生了什么。我会这样做,但这是一门数据结构课程,所以我不允许:-(您可以添加“家庭作业”)标记,这样每个人都知道不能完全为你解决问题。那你为什么不问问你的课程老师呢?那是你付钱的地方,对吧?祝你好运!我计划,但我只是想自己理解它,在写下变量值,并在纸上检查代码之后,它似乎起作用了,但出于某些原因,我无法实现它on.你是对的,我本想用I+1,而不是I+I,我纠正了无限循环(可能)的问题。我仍然不知道我的问题是什么,即使在解决了这个问题之后,我仍然会遇到同样的错误。我可能偶然发现了什么。我编写了一个测试:ArrayList a=new ArrayList();a.add(1);a.set(1,0);这会在线程“main”java.lang.IndexOutOfBoundsException:索引:1,大小:1中引发异常。这怎么可能?List.set“用指定的元素替换此列表中指定位置的元素”。当您尝试设置7时,位置1中没有任何内容,因此没有内容可替换。这是导致崩溃的原因。在进入for循环之前,您需要向列表中添加一个新项目(这会将其添加到列表的末尾)。然后当您进行移位时,会有一个数字的位置。您还需要将for循环更改为
for(int i=list.size()-2;i>=position;i--){
,因为列表的大小在此点之前增加了一。