插入部分填充的数组
Insertion into a partially filled array
我正在编写两种方法,一种用于从部分填充的数组中删除,另一种用于将元素插入部分填充的数组。我已设法完成删除;但插入给我带来了问题。对于我的两个结果,输出符合我的预期结果,除了数组末尾有零。我已经阅读了大量关于插入的文章,并在本网站的其他地方进行了检查,但找不到任何可以帮助我解决这个问题的东西。我只需要知道为什么零不断出现在输出中,以及 hints/ideas 我能做些什么来修复它。
我的代码:
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,您的 "global" 变量。
(2) 将长度作为参数传递给 printList,而不是直接引用 currentSize,您可能会获得更多样式点。由您决定是否修复,程序将 运行 无论哪种方式。我想我是说绝对最好将 intList 和 currentSize 作为参数传递,例如(将它们作为 arr 和 size 接收),否则不要传入并直接使用 intList 和 currentSize。
希望对您有所帮助!
我正在编写两种方法,一种用于从部分填充的数组中删除,另一种用于将元素插入部分填充的数组。我已设法完成删除;但插入给我带来了问题。对于我的两个结果,输出符合我的预期结果,除了数组末尾有零。我已经阅读了大量关于插入的文章,并在本网站的其他地方进行了检查,但找不到任何可以帮助我解决这个问题的东西。我只需要知道为什么零不断出现在输出中,以及 hints/ideas 我能做些什么来修复它。
我的代码:
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,您的 "global" 变量。
(2) 将长度作为参数传递给 printList,而不是直接引用 currentSize,您可能会获得更多样式点。由您决定是否修复,程序将 运行 无论哪种方式。我想我是说绝对最好将 intList 和 currentSize 作为参数传递,例如(将它们作为 arr 和 size 接收),否则不要传入并直接使用 intList 和 currentSize。
希望对您有所帮助!