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

This is a part of the code from TestMyArrayList: try { MyCollection<String> tmp2

ID: 3606855 • Letter: T

Question

This is a part of the code from TestMyArrayList:

try {

MyCollection<String> tmp2 = arrS.subList(-1, 1);

} catch (IndexOutOfBoundsException e) {

System.out.println("Index Out Of Bounds");

}

try {

MyCollection<String> tmp2 = arrS.subList(1, 100);

} catch (IndexOutOfBoundsException e) {

System.out.println("Index Out Of Bounds");

}

try {

MyCollection<String> tmp2 = arrS.subList(2, 1);

} catch (IllegalArgumentException e) {

System.out.println("Illegal Argument Exception");

}

MyCollection<String> tmp2 = arrS.subList(1, 4);

System.out.println(" Display Contents of sublist");

for (String s : tmp2) {

System.out.println(s);

}

System.out.println();

System.out.println("Results from removeAll: " + arrS.removeAll(tmp2)); // true

for (String s : arrS) {

System.out.println(s);

}

System.out.println(" Example of using the replaceAll method: ");

// Add an 's' to each String

arrS.replaceAll(s -> new String(s + "s"));

System.out.println();

for (String s : arrS) {

System.out.println(s);

}

MyCollection<Person> arrP = new MyArrayList<>();

arrP.add(new Person("Pilar", "Ess", 33));

arrP.add(new Person("Max", "Smith", 50));

arrP.add(new Person("Max", "Smith", 50));

arrP.add(new Person("Max", "Smith", 50));

arrP.add(new Person("Graham", "Monday", 16));

arrP.add(new Person("Diane", "Jones", 40));

arrP.add(new Person("Diane", "Jones", 40));

arrP.add(new Person("Cindy", "R", 22));

System.out.println(" Use forEach to display the Person list: ");

-------------------------------------------------------------

I need a help to write a sublist for tmp2 in MyArrayList

ANy suggestion how to do that....

Explanation / Answer

Please find my implementation.

// inclusive start, exclusive end

MyCollection<String> subList(int start, int end) {

MyCollection<String> sList = new MyArrayList<>();

for(int i=start, i<end; i++)

sList.add(arrS.get(i));

return sList;

}

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