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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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<String> names = new ArrayList<String>();
       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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education