对java赋值方法感到困惑

对java赋值方法感到困惑,java,arrays,methods,data-structures,Java,Arrays,Methods,Data Structures,大家好,现在我在学校学习数据结构,我很难理解我们的家庭作业是基于老师创建的代码 基本上,它是一个程序,用来存储整数,并根据它们的正、负和一个跟踪天气的集合来分配它们。整数的最后一个条目是什么。我们有一个类,它创建了一个新数组,并存储传递给它的任何int。我们还有一个insert方法,仅当int不在集合中时才将其插入集合。代码如下: public IntColl1(int i){ c = new int[i+1]; c[0] = 0; } public void insert(i

大家好,现在我在学校学习数据结构,我很难理解我们的家庭作业是基于老师创建的代码

基本上,它是一个程序,用来存储整数,并根据它们的正、负和一个跟踪天气的集合来分配它们。整数的最后一个条目是什么。我们有一个类,它创建了一个新数组,并存储传递给它的任何int。我们还有一个insert方法,仅当int不在集合中时才将其插入集合。代码如下:

public IntColl1(int i){
    c = new int[i+1];
    c[0] = 0;
}

public void insert(int i){
    if (i > 0){
        int j = 0;
        while ((c[j] != 0) && (c[j] != i)) j++;
        if (c[j] == 0){
            if (j == c.length - 1){
                int newLength = (c.length * 2);
                int[] d = new int[newLength];
                for(int l = 0; l < c.length; l++){
                    d[l] = c[l];
                }
                c = d;
            }
            c[j] = i;
            c[j + 1] = 0;
        }
    }
}
public IntColl1(inti){
c=新整数[i+1];
c[0]=0;
}
公共空白插入(int i){
如果(i>0){
int j=0;
而((c[j]!=0)&(c[j]!=i))j++;
如果(c[j]==0){
如果(j==c.length-1){
int newLength=(c.length*2);
int[]d=新的int[newLength];
对于(int l=0;l

它应该将Int插入数组中,如果数组太小,它将创建一个长度加倍的新数组。对于我们设置Intcoll1(1)的测试,有人能解释第一个if语句中的while循环吗?我一直认为,当创建一个空数组时,插槽都被设置为0,如果是这样,那么while循环不总是为false吗?

似乎while循环正在尝试查找数组中的下一个“可用”空间。最初(正如您正确指出的),while条件将立即为false。经过几次插入后,它将更加有用

假设初始数组大小为5,
c
看起来像

[0,0,0,0,0]
如果调用
insert(4)
,则条件

(c[j] != 0) && (c[j] != i)
失败,因此
j
永远不会递增,4作为位置0插入。现在
c
看起来像

[4,0,0,0,0]
现在,如果我调用
insert(7)
,当
j
为0时,条件将最初满足,但当
j
增加到1时,条件将失败,因此7将作为位置1插入:

[4,7,0,0,0]

while循环似乎正在尝试查找数组中的下一个“可用”空间。最初(正如您正确指出的),while条件将立即为false。经过几次插入后,它将更加有用

假设初始数组大小为5,
c
看起来像

[0,0,0,0,0]
如果调用
insert(4)
,则条件

(c[j] != 0) && (c[j] != i)
失败,因此
j
永远不会递增,4作为位置0插入。现在
c
看起来像

[4,0,0,0,0]
现在,如果我调用
insert(7)
,当
j
为0时,条件将最初满足,但当
j
增加到1时,条件将失败,因此7将作为位置1插入:

[4,7,0,0,0]
有人能解释一下第一个if语句中的while循环吗

我觉得理解上面的程序有问题。因此,我正在重新编写代码,以向您说明:

public void insert(int i){
    if (i > 0){
        int j = 0;
        while ((c[j] != 0) && (c[j] != i)){ //Introduced this bracket to show the start of while
           j++;
        } //Introduced this bracket to show the end of while
        if (c[j] == 0){
            if (j == c.length - 1){
                int newLength = (c.length * 2);
                int[] d = new int[newLength];
                for(int l = 0; l < c.length; l++){
                    d[l] = c[l];
                }
                c = d;
            }
            c[j] = i;
            c[j + 1] = 0;
        }
    }
}
public void insert(inti){
如果(i>0){
int j=0;
while((c[j]!=0)和&(c[j]!=i)){//引入此括号以显示while的开始
j++;
}//引入此括号以显示while的结尾
如果(c[j]==0){
如果(j==c.length-1){
int newLength=(c.length*2);
int[]d=新的int[newLength];
对于(int l=0;l
下一个if语句
if(c[j]==0)
不在
while
循环内。希望你现在明白了

有人能解释一下第一个if语句中的while循环吗

我觉得理解上面的程序有问题。因此,我正在重新编写代码,以向您说明:

public void insert(int i){
    if (i > 0){
        int j = 0;
        while ((c[j] != 0) && (c[j] != i)){ //Introduced this bracket to show the start of while
           j++;
        } //Introduced this bracket to show the end of while
        if (c[j] == 0){
            if (j == c.length - 1){
                int newLength = (c.length * 2);
                int[] d = new int[newLength];
                for(int l = 0; l < c.length; l++){
                    d[l] = c[l];
                }
                c = d;
            }
            c[j] = i;
            c[j + 1] = 0;
        }
    }
}
public void insert(inti){
如果(i>0){
int j=0;
while((c[j]!=0)和&(c[j]!=i)){//引入此括号以显示while的开始
j++;
}//引入此括号以显示while的结尾
如果(c[j]==0){
如果(j==c.length-1){
int newLength=(c.length*2);
int[]d=新的int[newLength];
对于(int l=0;l

下一个if语句
if(c[j]==0)
不在
while
循环内。希望您现在能理解。

对不起!这是我的第一个问题,所以还是习惯于它吧!这是我的第一个问题,所以我还是习惯了这是怎么给我们的,我重写了它,加上括号,但为了这个,我只是复制了老师给我们的us@10marcer那么你理解它有什么问题?它就是这样给我们的,我重写了它,加上括号,但为了这个,我只是复制了老师给我的东西us@10marcer那么你理解它的问题是什么?