import java.util.ArrayList; public class BasicArrayList {     // main method public static void main(String[] args) { // create an arraylist of type Integer ArrayList basic = new ArrayList(); // using loop add elements for(int i=0;i<4;i++){ basic.add(i+1); } // using loop set elements for(int i=0;i

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

API documentation link ------> https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html

PREVIOUS CODE -

import java.util.ArrayList;
public class BasicArrayList {
    // main method

public static void main(String[] args) {

// create an arraylist of type Integer

ArrayList<Integer> basic = new ArrayList<Integer>();


// using loop add elements

for(int i=0;i<4;i++){

basic.add(i+1);

}


// using loop set elements

for(int i=0;i<basic.size();i++){

basic.set(i,(basic.get(i)*5));

}


// using loop print elements

for(int i=0;i<basic.size();i++){

System.out.println(basic.get(i));

}

}
}

Q1)

a)Consider how to remove elements from the ArrayList. Give two different lines of code that would both remove 5 from the ArrayList.

b)What do you think the contents of the ArrayList will look like after the 5 has been removed? (Sketch or list)

c)Now update the BasicArrayList program to remove the 5 (index 1) from the ArrayList. Add this step before the last loop that prints out the contents fo the ArrayList. Examine the output and compare it to your answer in the previous question? Were there any surprises (or bugs) in your code?

d) Now go back and examine the API documentation for the ArrayList class (Links to an external site.). 

      i) Give an example line of code, calling an ArrayList method that would help you search the ArrayList for a specific element.  

      ii) What will this method return if the element is not found?

e)Referring back to the API documentation for the ArrayList class (Links to an external site.), 

     i) Give an example line of code that calls an ArrayList method that would remove all elements from the ArrayList.

     ii) Give an example line of code that calls an ArrayList method that would check if the ArrayList has no elements in it.

f)

Update your BasicArrayList program by adding the following:

  1. before the last for-loop, add a statement to set the element in position 0 to 1
  2. after the last for-loop add code to s
    1. search the ArrayList for the number 5 and display the index if found
    2. search the ArrayList for the number 15 and display the index if found
    3. check if the array is empty and print an appropriate message (e.g.,  "ArrayList is empty/not empty")
### Additional Functionality of ArrayLists

Besides the ability to resize, ArrayLists have additional functionality not present for arrays. These include:

| Method (Parameters)    | Return Value Type | Description                                                                  |
|------------------------|-------------------|------------------------------------------------------------------------------|
| remove(int index)      | E                 | Removes and returns the element at the specified position in this list.       |
| remove(Object o)       | boolean           | Removes the first occurrence of the specified element from this list, if it is present. |

There are many other ArrayList methods which you can read about in [the API documentation](https://link_to_API_documentation).

Recalling from the previous activity, the contents of the ArrayList after step 5 on the last question (where we converted the BasicArray program to BasicArrayList and a new element is added to position 0), are as follows:

| 0  | 5  | 10 | 15 | 20 |
|----|----|----|----|----|

In this table, each cell represents an element in the ArrayList at positions 0 through 4.

For further reading and more advanced methods, refer to the ArrayList API documentation linked above.
Transcribed Image Text:### Additional Functionality of ArrayLists Besides the ability to resize, ArrayLists have additional functionality not present for arrays. These include: | Method (Parameters) | Return Value Type | Description | |------------------------|-------------------|------------------------------------------------------------------------------| | remove(int index) | E | Removes and returns the element at the specified position in this list. | | remove(Object o) | boolean | Removes the first occurrence of the specified element from this list, if it is present. | There are many other ArrayList methods which you can read about in [the API documentation](https://link_to_API_documentation). Recalling from the previous activity, the contents of the ArrayList after step 5 on the last question (where we converted the BasicArray program to BasicArrayList and a new element is added to position 0), are as follows: | 0 | 5 | 10 | 15 | 20 | |----|----|----|----|----| In this table, each cell represents an element in the ArrayList at positions 0 through 4. For further reading and more advanced methods, refer to the ArrayList API documentation linked above.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Arrays
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