Java 当我尝试在排序后的数组中插入值时,得到的默认值为0

Java 当我尝试在排序后的数组中插入值时,得到的默认值为0,java,Java,我有一个大小为4的整数数组。我通过add方法向它添加元素。这是一个未排序的数组。我通过下面代码中显示的排序方法对其进行排序。排序方法将最小的数字放在位置a[0]中。当我尝试在调用sort方法后添加元素时,我总是得到一个返回值0。有办法解决这个问题吗 import java.util.Arrays; public class Scrap { private static int[] array = new int[4]; private static int i =

我有一个大小为4的整数数组。我通过add方法向它添加元素。这是一个未排序的数组。我通过下面代码中显示的排序方法对其进行排序。排序方法将最小的数字放在位置a[0]中。当我尝试在调用sort方法后添加元素时,我总是得到一个返回值0。有办法解决这个问题吗

  import java.util.Arrays;

    public class Scrap {
    private static int[] array = new int[4];
    private static int i = 0;

    public static void main(String[] args) {
        Scrap pq = new Scrap();
        pq.add(4);
        pq.insert(3);
        pq.add(5);

        pq.sort();// smallest to largest sort method.
        // System.out.println(array[0]);
        pq.insert(1);
        pq.sort();
        int test = pq.Minimum();
        System.out.println("The smallest element of the array is " + test);
        pq.sort();
    }

    //
    public void add(int input) {
        insert(input);
    }

    // Method to insert number into the array.
    public void insert(int input) {
        array[i] = input;
        i++;
    }

    // Finding smallest number of the array.
    public int Minimum() {
        int a = array[0];
        return a;
    }

    // Sorts the array from smallest to largest integer
    public void sort() {
        int first, temp;
        for (int i = array.length - 1; i > 0; i--) {
            first = 0;
            for (int j = 1; j <= 1; j++) {
                if (array[j] > array[first])
                    first = j;
            }
            temp = array[first];
            array[first] = array[i];
            array[i] = temp;
        }

    }

    public int remove() {
        return delete();
    }

    public int delete() {
        return remove();
    }
    // Method to convert the array into a string for output
}
导入java.util.array;
公共类废料{
私有静态int[]数组=新int[4];
私有静态int i=0;
公共静态void main(字符串[]args){
废品pq=新废品();
pq.增加(4);
pq.插入(3);
pq.增加(5);
pq.sort();//最小到最大排序方法。
//System.out.println(数组[0]);
pq.插入(1);
pq.sort();
内部测试=pq.最小值();
System.out.println(“数组的最小元素为”+test);
pq.sort();
}
//
公共无效添加(整数输入){
插入(输入);
}
//方法将数字插入数组中。
公共空白插入(整数输入){
数组[i]=输入;
i++;
}
//查找数组的最小数目。
公共整数最小值(){
int a=数组[0];
返回a;
}
//将数组从最小整数排序为最大整数
公共无效排序(){
首先是int,temp;
对于(int i=array.length-1;i>0;i--){
第一个=0;
对于(int j=1;j数组[第一])
第一个=j;
}
temp=数组[第一个];
数组[first]=数组[i];
数组[i]=温度;
}
}
公共int-remove(){
返回delete();
}
公共int delete(){
返回删除();
}
//方法将数组转换为字符串以进行输出
}

我假设您将使用正确的
排序方法(因为这不正确,您可以使用Arrays.sort)。但如果排序正确,代码中仍然存在逻辑问题

在开始时,数组包含所有0。添加前3个int后,当调用sort方法时,数组按以下顺序包含值:

0,3,4,5
注意,
i
的值没有改变。在这种状态下,i的值为3。因此,当您插入1时,新值将变为

0,3,4,1
因此,在再次排序之后,数组的值变为

0,1,3,4

因此,很明显,最小值将重新运行0,简而言之:

  • 从长度为4的数组开始。
    • 此时数组包含4个零,即:
      [0,0,0,0]
  • 添加4、3和5。这些操作将数组的内容更新为
    [4,3,5,0]
  • 您可以对数组进行排序。这会将数组的内容更改为
    [0,3,4,5]
    。事实上,它变为
    [0,5,3,4]
    ,这意味着
    排序的实现显然被破坏了。
    
    • 您可能没有想到0值会移动您可以通过仅对前3个值进行排序来解决此问题。(当然,您还应该修复
      排序的实现。
  • 然后,当您插入1时,程序将更新索引3处的值,因此内容将更改为
    [0,5,3,1]
如果您实现了我上面建议的修复,并且只对第一个
大小
元素进行排序,那么第一次调用
排序
后的内容应该变成
[3,4,5,0]
,插入1后的内容应该变成
[3,4,5,1]
。当你再次排序时,内容应该变成
[1,3,4,5]
,最小值应该是1,而不是0

更具体地说:

  • 首先,change
    private static int i=0
    私有int size=0。这里的名字
    i
    非常不合适,肯定会把你弄糊涂<代码>大小
    合适。将其设置为静态也没有意义,因此我建议删除该关键字
  • 修复
    排序的实现
    。有许多基本的排序算法易于实现。在实现中,不要等到
    array.size
    ,而是等到
    size
    。你看到区别了吗<代码>大小
报废
中的字段,本质上是使用
添加
插入
方法添加的元素数 清理一下也不错:

  • 删除
    add
    方法并将
    insert
    重命名为
    add
  • 删除
    remove
    Delete
    方法。它们没有被使用,如果您尝试像现在这样使用它们,您将得到堆栈溢出(这些方法永远互相调用)

在程序的每个步骤后查看数组的内容

创建
报废pq
后,这是其数组的内容:

[0, 0, 0, 0]
然后进行一些修改:

此时的内容:

[4, 3, 5, 0]
[0, 5, 3, 4]
[0, 5, 3, 1]
到目前为止还不错

然后对其进行排序:

此时的内容:

[4, 3, 5, 0]
[0, 5, 3, 4]
[0, 5, 3, 1]
哎哟。排序实现工作得不是很好,是吗。但我们暂时忽略这一点。下一步:

此时的内容:

[4, 3, 5, 0]
[0, 5, 3, 4]
[0, 5, 3, 1]

这些行为都没有意义,可能这不是您希望程序工作的方式。检查程序,在每个步骤后验证内容。在当前步骤正常工作之前,不要继续执行下一步。

我认为这不是对数组进行排序的最有效方法。只需1个for循环就可以实现这一点。尝试将数组从最小到最大排序

int temp;
for(int i=0;i<array.length-1;i++){
if(array[i]>array[i+1]){
    temp=array[i];
    array[i]=array[i+1];
    array[i+1]=temp;
    i=-1;
}
    }
int-temp;
对于(int i=0;iarray[i+1]){
温度=阵列[i];
数组[i]=数组[i+1];
阵列[i+1]=温度;
i=-1;
}
}

为什么你认为它应该是零以外的值?我不确定这就是我来这里的原因