尝试在排序数组中插入元素时出现错误

时间:2018-12-19 10:32:52

标签: java

我试图在数组中添加一个元素,然后对其进行排序。但是,当我尝试将元素再次插入已排序的数组时,那一次它向我显示了一个数组。请帮助我了解如何在排序数组中插入元素。

我的代码在下面。

import java.util.Scanner;

public class InsertionSort {
    public static void main(String[] args) {
        int n, temp, i, count = 0;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter number of Elements");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements");

        for (i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }

        for (i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }

        System.out.println("Ascending Order is :");

        for (i = 0; i < n - 1; i++) {
            System.out.print(a[i] + ",");
        }

        System.out.println(a[n - 1]);
        System.out.println("Select the number which you want to delete : ");
        int del = s.nextInt();

        for (i = 0; i < n; i++) {
            if (a[i] == del) {
                for (int j = i; j < (n - 1); j++) {
                    a[j] = a[j + 1];
                }

                count++;
                break;
            }
        }

        System.out.print("\nNow the New Array is :\n");

        for (i = 0; i < (n - 1); i++) {
            System.out.println(a[i] + " ");
        }

        System.out.println("Write a number which you want to insert :");

        int insert = s.nextInt();

        for (i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (insert > a[j]) {
                    temp = insert;
                    insert = a[j];
                    a[j] = temp;
                }
            }
        }

        System.out.println("Ascending Order is :");
        System.out.print(insert + ",");     
        System.out.println(a[n + 1]);
    }   
}

直到添加元素和删除都可以正常工作,但是插入新元素不能正常工作。

5 个答案:

答案 0 :(得分:1)

public static void insertIntoSortedArray(int[] arrSorted, int val) {
    int i = 0;

    // find insert position - i
    while (i < arrSorted.length - 1 && arrSorted[i] < val) {
        i++;
    }

    // shift array 1 position right (ignore last element)
    if (i < arrSorted.length - 1)
        System.arraycopy(arrSorted, i, arrSorted, i + 1, arrSorted.length - i - 1);

    // insert 1 element
    arrSorted[i] = val;
}

演示:

int[] arr = { 1, 2, 3, 5, 6 };
insertIntoSortedArray(arr, 4);  // [1, 2, 3, 4, 5]
insertIntoSortedArray(arr, 7);  // [1, 2, 3, 4, 7]
insertIntoSortedArray(arr, -1); // [-1, 1, 2, 3, 4]

答案 1 :(得分:0)

代码的最后一行导致ArrayIndexOutOfBoundsException:

System.out.println(a[n + 1]);

应该是类似

System.out.println(a[j + 1]); 

或者您想要打印的任何东西,也许是整个阵列?

答案 2 :(得分:0)

尝试使用a [n-1]代替a [n + 1],这将使您摆脱绑定异常

答案 3 :(得分:0)

问题在最后一个字符串中。您在数组中的最后一个索引为 n-1 Java Language Specification第10.4章

答案 4 :(得分:0)

following line is causing error;

System.out.println(a[n + 1])

instead write;

for (int k = 0; k < n; k++)
        System.out.println(a[k]);

by adding code above, your error(ArrayIndexOutOfBound) will be solved and code will work, but there is logical error in your code that resultant array is not correctly sorted. Which you've to solve. :)