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

Array lists are objects that, like arrays, provide you the ability to store item

ID: 3678651 • Letter: A

Question

Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let’s start with the code below: import java.util.ArrayList; public class ArrayListRunner public static void main(String[] args) { ArrayList names = new ArrayList(); System.out.println(names); } The main method imports java.util.ArrayList and creates an ArrayList that can hold strings. It also prints out the ArrayList and, when it does, we see that the list is empty: [ ]. Complete the following tasks by adding code to this skeleton program. If you are asked to print a value, provide a suitable label to identify it when it is printed.

a) Invoke add() to enter the following names in sequence: Alice, Bob, Connie, David, Edward, Fran, Gomez, Harry. Print the ArrayList again.

b) Use get() to retrieve and print the first and last names.

c) Print the size() of the ArrayList.

d) Use size() to help you print the last name in the list.

e) Use set() to change “Alice” to “Alice B. Toklas”. Print the ArrayList to verify the change.

f) Use the alternate form of add() to insert “Doug” after “David”. Print the ArrayList again.

g) Use an enhanced for loop to print each name in the ArrayList.

h) Create a second ArrayList called names2 that is built by calling the ArrayList constructor that accepts another ArrayList as an argument. Pass names to the constructor to build names2. Then print the ArrayList.

i) Call names.remove(0) to remove the first element. Print names and names2. Verify that Alice B. Toklas was removed from names, but not from names2.

Explanation / Answer

import java.util.ArrayList;

public class ArrayListRunner

{

   public static void main(String[] args)

   {

       ArrayList<String> names = new ArrayList<String>();

//invoking add() and adding names into names ArrayList

names.add("Alice ");

names.add("Bob");

names.add("Connie");

names.add("David");

names.add("Edward");

names.add("Fran");

names.add("Gomez");

names.add("Harry");

//printing elements in arrayList names

System.out.println(names);

//getting last and first elements names

String sf=list.get(0);//list is the interface implements by arrayList

String sl= list.get(list.size() - 1);

System.out.println("First element in the Arraylist is "+sf);

System.out.println("Last element in the Arraylist is "+sl);

int size=names.size();

//printing size of arraylist

System.out.println("The size of Arraylist is "+size);

// Use size() to help you print the last name in the list.

String sl1 = list.get(list.size() - 1);

System.out.println("Last element in the Arraylist is "+sl1);

// Use set() to change “Alice” to “Alice B. Toklas”

list.set(0, " Alice B. Toklas ");

// Printing the ArrayList to verify the change.

System.out.println(names);

// Use the alternate form of add() to insert “Doug” after “David”

list.add( 4, "Doug" );

System.out.println(names);

// enhanced for loop to print each name in the ArrayList.

for (int index=0; index < names.size(); index++)

        {

            System.out.println(names.get(index));

        }

       ArrayList<String> names2 = new ArrayList<String>(names);

   names2.add("Honey");

names.remove(0);//removing first element in names arraylist

//printing names names2 Arraylist s

System.out.println(names);

System.out.println(names2);

}

}

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