Add a main so that you can put some code there to call your functions and make s
ID: 3883114 • Letter: A
Question
Add a main so that you can put some code there to call your functions and make sure they work:
public static void main(String[] args) { /* your testing code goes here */ }
Functions you should add if the functionality is possible. Otherwise add an empty function definition with a comment saying "impossible".
public static void removeEveryOther(ArrayList<Integer> values) {
/* This function should update in-place the values arraylist so that after returning, the values arraylist only contains every other value contained when the function was called. In other words, before returning, the items originally at positions 1, 3, 5, 7… should no longer exist in the values arraylist and the arraylist should be ½ the size.. */
}
** Test input: an empty ArrayList, an ArrayList with 5 values, and ArrayList with 6 value. If you make the values in the arraylist consecutive, it'll be very easy to see whether you are right; for example: 6, 7, 8, 9, 10.
public static void removeEveryOther(int[] values) {
/* This function should update in-place the values array so that after returning, the values array only contains every other value contained when the function was called. In other words, before returning, the items originally at positions 1, 3, 5, 7… should no longer exist in the values array and the array should be ½ the size. */
}
** Test input: same as #1
public static ArrayList<Integer> getEveryOther(ArrayList<Integer> values) {
/* This function should return a new arraylist that contains every other value contained in the values arraylist. In other words, the function should return a new arraylist containing the values from positions 0, 2, 4, 6…. and input arraylist values should remain unchanged. */
}
** Test input: same as #1
Explanation / Answer
public static void removeEveryOther(ArrayList<Integer> values) {
int len = values.size();
/* This function should update in-place the values arraylist so that after returning, the values arraylist only contains every other value contained when the function was called. In other words, before returning, the items originally at positions 1, 3, 5, 7… should no longer exist in the values arraylist and the arraylist should be ½ the size.. */
for(int i = 1; i < len; i = i+2)
{
values.remove(i);
}
}
public static void removeEveryOther(int[] values) {
/* This function should update in-place the values array so that after returning, the values array only contains every other value contained when the function was called. In other words, before returning, the items originally at positions 1, 3, 5, 7… should no longer exist in the values array and the array should be ½ the size. */
// impossible as there is no mechanism to reduce size of array inplace
}
public static ArrayList<Integer> getEveryOther(ArrayList<Integer> values) {
/* This function should return a new arraylist that contains every other value contained in the values arraylist. In other words, the function should return a new arraylist containing the values from positions 0, 2, 4, 6…. and input arraylist values should remain unchanged. */
int len = values.size();
ArrayList<Integer> result = new ArrayList<>();
for(int i = 0; i < len; i = i+2)
{
result.add(values.get(i));
}
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.