java - 插入部分填充的数组

标签 java arrays insertion-sort

我正在编写两种方法,一种用于从部分填充的数组中删除,另一种用于将元素插入部分填充的数组中。我已经成功完成删除;但插入给我带来了问题。对于我的两个结果,输出与我的预期结果相匹配,但数组末尾有零。我已经阅读了大量有关插入的文章,并在本网站的其他地方进行了检查,但找不到任何可以帮助我解决此问题的内容。我只需要知道为什么零不断出现在输出中以及关于我可以采取哪些措施来修复它的提示/想法。

我的代码:

import static java.lang.System.exit;
import static java.lang.System.out;
import java.util.Scanner;

public class ICA01_CC_PartB {

    final static Scanner cin = new Scanner(System.in);
    static int currentSize; // number of values actually in the intList
    static int[] intList;   // reference to the partially filled array storage

    public static void main(String[] args) {
        out.println("CPS 151 ICA 1 Part B");
        setup();
        printList(intList, "\nOriginal List");
        checkInsertion();
        out.println("\nGoodbye");
    } // end main

    private static void checkInsertion() {
        // check if there is room to insert
        if (currentSize >= intList.length) {
            terminate("List is full, cannot insert");
        }
        // Checking insertion
        int value = getInt("\nValue to insert: ");
        int position = getInt("At what index position? ");

        // check validity of position
        // TODO Put correct validation check
        if (position >= 0 && position <= currentSize) {
        shiftDown(position);
            intList[position] = value;
            currentSize++;
            printList(intList, "\nList after insertion");
        } else {
            out.println("Invalid insert position, no changes made");
        } // end if
    } // end method

    // move items from pos:currentSize-1 one position down (higher subscripts)
    private static void shiftDown(final int pos) {
      // TODO Write the code
      for(int i = (pos - 1); i >= 0 && i >= currentSize; i--){
             intList[i + 1] = intList[i];
          }
   } // end shiftDown

    // fills array with increasing values
    private static void fillArrayInc(final int startValue, final int howMany) {
        // Validity check 
        if (howMany < 0 || howMany > intList.length) {
            terminate("fillArrayInc: illegal argument, howMany = " + howMany);
        }

        for (int k = 0; k < howMany; k++) {
            intList[k] = startValue + k;
        }
        currentSize = howMany;
    } //end fillArrayInc

    // prints partially filled array with a legend
    private static void printList(final int[] arr, final String legend) {
        out.println(legend);
        out.print('[');
        // print first list item for a non-empty list
        if (currentSize > 0) {
            out.print(intList[0]);
        }
        // print rest of list items, comma separated
        for (int k = 1; k < currentSize; k++) {
            out.print(", " + arr[k]);
        }
        out.println(']');
    } // end printList

    private static void setup() {
        int maxSize, initSize;
        maxSize = getInt("Enter the maximum size: ");
        intList = new int[maxSize];
        initSize = getInt("Enter the starting size: ");
        if (initSize > maxSize) {
            terminate("starting size cannot be greater than maximum size");
        }
        fillArrayInc(100, initSize);
    } // end method

    private static int getInt(String prompt) {
        out.print(prompt);
        return cin.nextInt();
    } // end method

    private static void terminate(String message) {
        out.println("Error: " + message);
        exit(0);
    } // end terminate

} // end class

输出:

Output image

最佳答案

我认为你的shiftDown函数不太正确。你不想要更多这样的东西吗? (即从最高位置开始向后移动?)

private static void shiftDown(final int pos) {    
  for (int i = currentSize; i >= pos; i--) {
    intList[i+1] = intList[i];
  }
} // end shiftDown

我还发现了一些小错误,它们都不会导致你的程序失败,但可能是提高你的成绩的方法: (1) 在 printList 例程中,您传入了一个数组 arr,但是对于 [0] 元素,您直接引用了“全局”变量 intList。 (2) 您可能会因为将长度作为参数传递给 printList 而获得更多样式点,而不是直接引用 currentSize。根据您是否修复,程序将以任何一种方式运行。我想我是说最好将 intList 和 currentSize 作为参数传递,例如(以 arr 和 size 的形式接收它们),否则不要传入并直接使用 intList 和 currentSize。

希望这有帮助!

关于java - 插入部分填充的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61399935/

相关文章:

java - 输入5个数字,然后显示您输入的所有正数和负数

Ruby 数组按多个属性排序

c - 数组的二进制插入排序不起作用(C 代码)

Windows 2003 与 Windows 2008 中的 Java Web 应用程序性能

Java反射-从非泛型类获取泛型方法

java - 如何在 Android 中创建多语言枚举?

c++ - 使用 C 或 C++ 的汇编语言

java - Android SAX解析空指针异常

java - System.currentTimeMillis 在 Eclipse 的 Junit Test 中无法正常工作

java - 在 Java 中写入文件时使用插入排序