import java.util.ArrayList;public class ArrayListDemo{ public static void main(S
ID: 3849172 • Letter: I
Question
import java.util.ArrayList;public class ArrayListDemo{ public static void main(String[] args) { ArrayList names = new ArrayList(); names.add("Abigail"); display(names); names.add("Brian"); display(names); names.add("Zachary"); display(names); names.add(2, "Christy"); display(names); names.remove(1); display(names); names.set(0, "Annette"); display(names); } public static void display(ArrayList names) { System.out.println(" The size of the list is " + names.size()); for(int x = 0; x < names.size(); ++x) System.out.println("position " + x + " Name: " + names.get(x)); }}
Re-write this program so that the names of the Seven Dwarfs ( Doc ; Dopey; Bashful; Grumpy; Sneezy;Sleepy; Happy) are used instead of the names in the program. Arrange the names in alphabetical order
Explanation / Answer
SOLUTION:
import java.util.ArrayList;
public class ArrayListDemo{
public static void main(String[] args) {
ArrayList names = new ArrayList();
names.add("Doc");
display(names);
names.add("Dopey");
display(names);
names.add("Bashful");
display(names);
names.add("Grumpy");
names.add("Sneezy");
names.add("Sleepy");
names.add("Happy");
System.out.println("Array list is sorted now ");
names.sort(null);
display(names);
}
public static void display(ArrayList names) {
System.out.println(" The size of the list is " + names.size());
for(int x = 0; x < names.size(); ++x)
System.out.println("position " + x + " Name: " + names.get(x));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.