I'm in the process of writing two methods, one for deleting from a partially filled array and the other from inserting an element into a partially filled array. I've managed to complete the deletion; but the insertion is giving me problems. For two of my results, the output matches my expected results excpet for there being zeroes at the end of the array. I've read numerous articles on insertion and checked elsewhere on this site but can't find anything that's helping me solve this. I just need to know why the zeroes keep showing up in the output and hints/ideas as to what I can do to fix it.
My code:
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:
I think your shiftDown function isn't quite right. Don't you want something more like this? (i.e. start at the highest position working your way backward?)
private static void shiftDown(final int pos) {
for (int i = currentSize; i >= pos; i--) {
intList[i+1] = intList[i];
}
} // end shiftDown
Also a couple minor inaccuracies I spotted, neither of which will make your program fail, but could be ways to improve your grade:
(1) in the printList routine, you've passed in an array arr
but then for the [0] element you directly reference intList, your "global" variable.
(2) you probably get more style points for also passing in the length to printList as a parameter, rather than just directly referencing currentSize. Up to you whether to fix, the program will run either way. I guess I'm saying it's definitely best to EITHER pass both intList and currentSize in as parameters e.g. (receive them as arr and size), or else don't pass either in and directly use intList and currentSize.
Hope this helps!