Question: Remember that and ArrayList is an array - based List ( ADT ) . You are implementing an ArrayList of integers. When the list is

Remember that and ArrayList is an array-based List (ADT).
You are implementing an ArrayList of integers. When the list is initialized all elements are set to zero.
When removing an element, the value of any empty elements will be reset to zero.
Can you only fill in the blanks and not touch imports?
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/**
* You need to be mindful of the capacity of your ArrayList, and may need to
* adjust it, before/after any of the following operation completes.
*/
interface IArrayList {
public int getSize();
public int getCapacity();
public int get(int index);
public void add(int value);
public void addAtIndex(int value, int index);
public void remove(int value);
public void removeAtIndex(int index);
/**
* This method will remove all the elements from your ArrayList.
*/
public void clear();
}
class ArrayList implements IArrayList {
private int[] arr;
// The maximum number of elements arr is able to hold, until resizing is
needed.
private int capacity;
// This will hold number of elements in your ArrayList. Notice it's different
// than the capacity!
private int size;
public ArrayList(int capacity){
this.capacity = capacity;
this.arr = new int[capacity];
}
public void print(){
String elements ="[";
for (int i =0; i < this.capacity; i++){
elements += this.arr[i];
if (i < this.capacity -1){
elements +=",";
}
}
elements +="]";
String summary = String.format("{ capacity: %d, size: %d, elements: %s }",
this.capacity, this.size, elements);
System.out.println(summary);
}
/***************************************************************************/
// Write your code below here...
/***************************************************************************/
// Use this as a helper method
private void doubleCapacity(){
// Write your code here...
}
// Use this as a helper method
private void halveCapacity(){
// Write your code here...
}
// implement the IArrayList.
// Write your code here...
}
class Solution {
public static void main(String[] args) throws IOException {
// input handling
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
int capacity = Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+
$","").split("=")[1].trim());
int operationCount =
Integer.parseInt(bufferedReader.readLine().replaceAll("\\s+$","").split("=")
[1].trim());
bufferedReader.readLine();
ArrayList arrList = new ArrayList(capacity);
IntStream.range(0, operationCount).forEach(opCountItr ->{
try {
List theInput =
Stream.of(bufferedReader.readLine().replaceAll("\\s+$","").split(","))
.collect(toList());
String action = theInput.get(0);
String arg1= theInput.size()>1? theInput.get(1).trim() : null;
String arg2= theInput.size()>2? theInput.get(2).trim() : null;
String arg3= theInput.size()>3? theInput.get(3).trim() : null;
ProcessInputs(arrList, action, arg1, arg2, arg3);
} catch (IOException exception){
throw new RuntimeException(exception);
}
});
bufferedReader.close();
}
private static void ProcessInputs(ArrayList arrList, String action, String
arg1, String arg2, String arg3){
int result;
int value;
int index;
switch (action){
case "getSize":
result = arrList.getSize();
System.out.println(result);
break;
case "getCapacity":
result = arrList.getCapacity();
System.out.println(result);
break;
case "get": {
index = Integer.parseInt(arg1);
result = arrList.get(index);
System.out.println(result);
break;
}
case "add":
value = Integer.parseInt(arg1);
arrList.add(value);
break;
case "addAtIndex":
value = Integer.parseInt(arg1);
index = Integer.parseInt(arg2);
arrList.addAtIndex(value, index);
break;
case "remove":
value = Integer.parseInt(arg1);
arrList.remove(value);
break;
case "removeAtIndex":
index = Integer.parseInt(arg1);
arrList.removeAtIndex(index);
break;
case "clear":
arrList.clear();
break;
case "print":
arrList.print();
break;
}
}
}
CAPACITY=10
OPERATION_COUNT=5
add, 1
add, 2
add, 3
addAtIndex, 4,0
print
Sample Output 3
{ capacity: 10, size: 4, elements: [4,1,2,3,0,0,0,0,0,0]}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!