Create a collection of ArrayList with a size of 100 String elements.

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

// The language is java, please take a screenshot of your output, and make sure your code is run.

5. Write code to create the following collections:
a.
Create a collection of ArrayList with a size of 100 String elements.
b. Use of a for-each loop to initialize each element in the above collection to "Java".
c. Create a collection of LinkedList and referred by List with size of 10 integer elements.
d. Use of a for-each loop to initialize all elements with values from 10, 20, 30, ..., to 100.
Use at least five methods provided in Table 13.7 in writing a driver code to manipulate in
some manner the collection created in the above example. Print each element in the
е.
collection. Run and save all files.
Transcribed Image Text:5. Write code to create the following collections: a. Create a collection of ArrayList with a size of 100 String elements. b. Use of a for-each loop to initialize each element in the above collection to "Java". c. Create a collection of LinkedList and referred by List with size of 10 integer elements. d. Use of a for-each loop to initialize all elements with values from 10, 20, 30, ..., to 100. Use at least five methods provided in Table 13.7 in writing a driver code to manipulate in some manner the collection created in the above example. Print each element in the е. collection. Run and save all files.
Expert Solution
Step 1

a )Creation of collection of Array List 

public class JExample{

public static void main(String args[]) {

//creating ArrayList of type String 

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

/Add elements to an ArrayList

obj.add("Ajeeth");

obj.add("Harray");

obj.add("Chaitanya");

obj.add("steve");

obj.add("Anuj");

//Displaying elements

System.out.println("Original ArrayList:");

for(String str:obj)

System.out.println(str);

//Add elements at the index

obj.add(0,"Rahul");

obj.add(1,"Justin");

//Displaying elements

System.out.println("ArrayList after add operation:");

for(String str: obj)

System.out.println(str);

//Remove elements from ArrayList like this

obj.remove("Chaitanya");

obj.remove("Harry");

//Displaying elements

System.out.println("ArrayList after remove operation:");

for(String str : obj)

System.out.println(str);

//Remove a  element from the specified index

obj.remove(1);

//Displaying elements

System.out.println("Final ArrayList:");

for(String str:obj)

System.out.println(str);

}

}

OUTPUT:

Computer Science homework question answer, step 1, image 1

 

 

 

Step 2

c ) Create a collection of linked list and initialize

import java.util.*;

public class JavaExample{

public static void main(String args[]) {

LinkedList<String> list= new LinkedList<String>();

//Adding elements to the Linked list

list.add("Steve");

list.add("Carl");

list.add("Raj");

//Adding an element to the first position

list.addFirst("Negan");

//Adding an element to the last position

list.addLast("Rick");

//Adding an element to the 3rd position

list.add(2,"Glenn");

//Iterating LinkedList

Iterator<String>iterator = list.iterator();

while(iterator.hasNext()) {

System.out.println(iterator.next());

}

}

}

OUTPUT:

Computer Science homework question answer, step 2, image 1

steps

Step by step

Solved in 3 steps with 2 images

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