public class Beatles { public static void main (String[] args) { ArrayList band
ID: 3632437 • Letter: P
Question
public class Beatles{ public static void main (String[] args)
{ ArrayList band = new ArrayList();
band.add("John");
band.add ("Paul");
band.add ("Pete");
band.add ("John");
band.add ("George");
band.add("John");
System.out.println (band);
int location = band.indexOf ("Pete");
band.remove (location);
System.out.println (band);
System.out.println ("At index 1: " + band.get(1));
band.add (2, "Ringo");
System.out.println (band);
System.out.println ("Size of the band: " + band.size());
// check to see if John is in the band. It should print true or false
// add John again
// find all of the indices where John occurs. Print these. HINT!!! There is
// no single method for this. You will need to loop through the ArrayList getting
// each entry and checking to see if it is John. Use the method .equals to compare
// Strings ---- not == (this can only be used for primitive data types)
// print out how many Beatles there are now
// clear out all the members of the ArrayList
// print out the ArrayList now
// add back in Paul, George, John, and Ringo
// print out the ArrayList
Explanation / Answer
// Beatles.java Author: Lewis/Loftus // // Demonstrates the use of a ArrayList object. //******************************************************************** import java.util.ArrayList; public class Beatles { //----------------------------------------------------------------- // Stores and modifies a list of band members. //----------------------------------------------------------------- public static void main (String[] args) { // ArrayList band = new ArrayList(); ArrayList band = new ArrayList(); band.add ("Paul"); band.add ("Pete"); band.add ("John"); band.add ("George"); System.out.println (band); int location = band.indexOf ("Pete"); band.remove (location); System.out.println (band); System.out.println ("At index 1: " + band.get(1)); band.add (2, "Ringo"); System.out.println (band); System.out.println ("Size of the band: " + band.size()); System.out.println ("The band: "); for (String str : band) System.out.println (str); } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.