My sample Mid-term asks me to write a method that takes an array called origArr and returns a copy of it. The returned copy should have a specified length called len and if len is greater than origArr's length than the extra slots are filled with zero. The question also states that no functions of the array class can be used. When I print the new array it doesn't add the zero's to the extra slots. So far my code is:
public static void main(String[] args) {
int[] origArr = {1,2,3,4,5,6};
int len = 0;
int[] array = myCopyOf(origArr, len);
for (int i=0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static int[] myCopyOf(int[] origArr, int len) {
//declare new array//
int[] myCopyOf = new int[origArr.length];
//declare length should be equal to origArr' length//
len = origArr.length;
if (len > origArr.length) {
for (int i = 0; i < origArr.length; i++) {
myCopyOf[i] = origArr[i] + 0;
}
}
return myCopyOf;
}
+0doesn't change the value and is a noop