java - 插入部分填充的数组
问题描述
我正在编写两种方法,一种用于从部分填充的数组中删除,另一种用于将元素插入到部分填充的数组中。我已成功完成删除;但插入给我带来了问题。对于我的两个结果,输出与我的预期结果匹配,因为数组末尾有零。我已经阅读了许多关于插入的文章,并在本网站的其他地方进行了检查,但找不到任何可以帮助我解决这个问题的东西。我只需要知道为什么零一直出现在输出中,以及我可以做些什么来修复它的提示/想法。
我的代码:
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
输出:
解决方案
我认为您的 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。
希望这可以帮助!