Have the user enter 5 String Objects and add them to an arraylist o String Objec
ID: 3691112 • Letter: H
Question
Have the user enter 5 String Objects and add them to an arraylist
o String Objects…Phone, Radar, Jungle, Building, & Trash
o Copy these String Objects to a 2nd arraylist
· Have the user pass the 1st arraylist to a EditString method
o Sort the arraylist in ascending order
o Print the arraylist and the size of the arraylist using the size method
o Using the removeRange method, remove the first 2 elements
o Print the arraylist
o Using the clear method, remove remaining elements of the list, print the size of the list
· Have the user pass the 2nd arraylist to SearchList Method
· Using the contains method, determine and print if the word Trash is in the arraylist
CODE IN JAVA, PLEASE MAKE SURE PROGRAM WORKS BEFORE POSTING< MUCH APPRECIATED
Explanation / Answer
ArrayListOperations.java
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListOperations {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> firstList = new ArrayList<String>();
firstList.add("Phone");
firstList.add("Radar");
firstList.add("Jungle");
firstList.add("Building");
firstList.add("Trash");
ArrayList<String> secondList = new ArrayList<String>(firstList);
editString(firstList);
printArrayList(firstList);
System.out.println("------------------");
removeRange (firstList, 2);
printArrayList(firstList);
System.out.println("----------------");
removeRange(firstList);
printArrayList(firstList);
System.out.println("------------------");
searchList(secondList);
}
public static void editString(ArrayList list){
/* Sort statement*/
Collections.sort(list);
}
public static void printArrayList(ArrayList list){
System.out.println("ArrayList Size "+list.size());
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
public static void removeRange(ArrayList list, int removeRange){
for(int i=0;i<removeRange;i++){
list.remove(i);
}
}
public static void removeRange(ArrayList list){
list.clear();
}
public static void searchList(ArrayList list){
boolean status = list.contains("Trash");
if(status)
System.out.println("ArrayList contains Trash");
else
System.out.println("ArrayList does not contain Trash");
}
}
Output:
ArrayList Size 5
Building
Jungle
Phone
Radar
Trash
------------------
ArrayList Size 3
Jungle
Radar
Trash
----------------
ArrayList Size 0
------------------
ArrayList contains Trash
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.