Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the existing ArrayLists\'s contents, by erasing 200, then inserting 100 a

ID: 3688740 • Letter: M

Question

Modify the existing ArrayLists's contents, by erasing 200, then inserting 100 and 102 in the shown locations. Use ArrayList's remove() and add() only. Sample output of below program:

**The second test uses 6 7 8 9

import java.util.ArrayList;

public class ArrayListADT {

public static void printArray(ArrayList<Integer> numsList, int numOfElem) {
int i = 0;

for (i = 0; i < numOfElem; ++i) {
System.out.print(numsList.get(i));
System.out.print(" ");
}

System.out.println("");
}

public static void main (String [] args) {
ArrayList<Integer> numsList = new ArrayList<Integer>();
int numOfElem = 4;

numsList.add(new Integer(101));
numsList.add(new Integer(200));
numsList.add(new Integer(103));

/* Your solution goes here */

printArray(numsList, numOfElem);

return;
}
}

Explanation / Answer

1.The existing ArrayLists's contents, by erasing 200, then inserting 100 and 102 in the locations.

import java.util.*;

import java.lang.*;

import java.io.*;

import java.util.ArrayList;

class Codechef {

public static void printArray(ArrayList<Integer> numsList, int numOfElem) {

int i = 0;

for (i = 0; i < numOfElem; ++i) {

System.out.print(numsList.get(i));

System.out.print(" ");

}

System.out.println("");

}

public static void main (String [] args) {

ArrayList<Integer> numsList = new ArrayList<Integer>();

int numOfElem = 4;

numsList.add(new Integer(100));

numsList.add(new Integer(101));

numsList.remove(new Integer(200));

numsList.add(new Integer(102));

numsList.add(new Integer(103));

printArray(numsList, numOfElem);

return;

}

}

2.The second test uses 6 7 8 9

import java.util.*;
import java.lang.*;
import java.io.*;


import java.util.ArrayList;
class Codechef {
public static void printArray(ArrayList<Integer> numsList, int numOfElem) {
int i = 0;
for (i = 0; i < numOfElem; ++i) {
System.out.print(numsList.get(i));
System.out.print(" ");
}
System.out.println("");
}
public static void main (String [] args) {
ArrayList<Integer> numsList = new ArrayList<Integer>();
int numOfElem = 4;
numsList.add(new Integer(6));
numsList.add(new Integer(7));
  
numsList.add(new Integer(8));
numsList.add(new Integer(9));
  
printArray(numsList, numOfElem);
return;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote