Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 11.11, Problem 11.11.3CP
Program Plan Intro

ArrayList:

A list of object is stored in the “ArrayList”. Once an array list is created, the size of the array is fixed. Java gives the “ArrayList” class for storing an unlimited number of objects in a single list.

Creation of ArrayList:

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

The above statement is an example of creation of “ArrayList”. The array object “x” is used to store the list of objects.

Explanation of Solution

Reason for not removing all the “Dallas” in the list:

No”, the given code is not correctly removes all the elements in the arrayList.

Justification:

In the above sub part code,

  • The variable “i” becomes “1” after removing one “Dallas” in the list and the variable “i” becomes “2” after removing two “Dallas” in the list. Then, the check the condition “2 < 2”. But it fails. So, the loop will end after removing two elements.
  • Hence, the given code is correctly not removes all the elements in the arrayList. Because, the final array list contains “{“Houston”, “Dallas”}”.

Corrected code:

//Import the java package

import java.util.*;

//Define the class

public class Sample

{

    //Define the main() method

public static void main(String[] args) throws Exception

    {

        //Declare the array list

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

        //Call add() method to add the string into list

        list.add("Dallas");

        list.add("Houston");

        list.add("Dallas");

        list.add("Dallas");

/*Check whether "i" is less than size of array list. */

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

        {

/*Check whether the condition is true. That is, removing the element from list by calling the remove() method. */

            if(list.remove("Dallas"))

            {

                //Decrement the "i" value

                i--;

/*Display the remaining elements in array list. */

                System.out.println(list);

            }

        }   

    }

}

Explanation:

In the above code,

  • Declare the array list in the name of “list”...

Blurred answer
Students have asked these similar questions
Direction: Continue the code below and add case 4, case 5, and case 6. Add 3 more functions aside from insert, getValue, and clear from List ADT import java.util.LinkedList; import java.util.Scanner; class SampleLL { } public static void main(String[] args) { LinkedList 11s = new LinkedList(); String msg = "Choose a function: \n [1] Insert, [2]Get Value, [3]Clear, [0] Exit"; System.out.println(msg); Scanner scan= new Scanner(System.in); int choice scan.nextInt (); while(true) { } if (choice =0) { } System.exit(0); switch(choice) { } case 1: System.out.println("Enter a word/symbol:"); break; case 2: System.out.println("Enter a number: "); break; 11s.add(scan.next()); break; case 3 11s.clear(); default: System.out.println("Invalid input!"); break; System.out.println(11s.get (scan.nextInt())); System.out.println(msg); choice scan.nextInt ();
Write a Python code using the given function and conditions. Do not use Numpy. Use LinkedList Manipulation.   Given function: def insert(self, newElement, index)  Pre-condition: The list is not empty. Post-condition: This method inserts newElement at the given index of the list. If an element with the same key as newElement value already exists in the list, then it concludes the key already exists and does not insert the key. [You must also check the validity of the index].
Make a doubly linked list and apply all the insertion, deletion and search cases. The node willhave an int variable in the data part. Your LinkedList will have a head and a tail pointer.Your LinkedList class must have the following functions: 1) insert a node1. void insertNodeAtBeginning(int data);2. void insertNodeInMiddle(int key, int data); //will search for keyand insert node after the node where a node’s data==key3. void insertNodeAtEnd(int data);2) delete a node1. bool deleteFirstNode(); //will delete the first node of the LL2. bool deleteNode(int key); //search for the node where its data==keyand delete that particular node3. bool deleteLastNode(); //will delete the last node of the LL3) Search a node1. Node* searchNodeRef(int key); //will search for the key in the datapart of the node2. bool searchNode(int key); The program must be completely generic, especially for deleting/inserting the middle nodes.   Implement all the functions from the ABOVE STATEMENTS and make a login and…

Chapter 11 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Ch. 11.5 - Identify the problems in the following code:...Ch. 11.5 - Prob. 11.5.2CPCh. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same signature...Ch. 11.5 - If a method in a subclass has the same name as a...Ch. 11.5 - Prob. 11.5.6CPCh. 11.7 - Prob. 11.7.1CPCh. 11.8 - Prob. 11.8.1CPCh. 11.8 - Prob. 11.8.2CPCh. 11.8 - Can you assign new int[50], new Integer [50], new...Ch. 11.8 - Prob. 11.8.4CPCh. 11.8 - Show the output of the following code:Ch. 11.8 - Show the output of following program: 1public...Ch. 11.8 - Show the output of following program: public class...Ch. 11.9 - Indicate true or false for the following...Ch. 11.9 - For the GeometricObject and Circle classes in...Ch. 11.9 - Suppose Fruit, Apple, Orange, GoldenDelicious, and...Ch. 11.9 - What is wrong in the following code? 1public class...Ch. 11.10 - Prob. 11.10.1CPCh. 11.11 - Prob. 11.11.1CPCh. 11.11 - Prob. 11.11.2CPCh. 11.11 - Prob. 11.11.3CPCh. 11.11 - Prob. 11.11.4CPCh. 11.11 - Prob. 11.11.5CPCh. 11.12 - Correct errors in the following statements: int[]...Ch. 11.12 - Correct errors in the following statements: int[]...Ch. 11.13 - Prob. 11.13.1CPCh. 11.14 - What modifier should you use on a class so a class...Ch. 11.14 - Prob. 11.14.2CPCh. 11.14 - In the following code, the classes A and B are in...Ch. 11.14 - In the following code, the classes A and B are in...Ch. 11.15 - Prob. 11.15.1CPCh. 11.15 - Indicate true or false for the following...Ch. 11 - Sections 11.211.4 11.1(The Triangle class) Design...Ch. 11 - (Subclasses of Account) In Programming Exercise...Ch. 11 - (Maximum element in ArrayList) Write the following...Ch. 11 - Prob. 11.5PECh. 11 - (Use ArrayList) Write a program that creates an...Ch. 11 - (Shuffle ArrayList) Write the following method...Ch. 11 - (New Account class) An Account class was specified...Ch. 11 - (Largest rows and columns) Write a program that...Ch. 11 - Prob. 11.10PECh. 11 - (Sort ArrayList) Write the following method that...Ch. 11 - (Sum ArrayList) Write the following method that...Ch. 11 - (Remove duplicates) Write a method that removes...Ch. 11 - (Combine two lists) Write a method that returns...Ch. 11 - (Area of a convex polygon) A polygon is convex if...Ch. 11 - Prob. 11.16PECh. 11 - (Algebra: perfect square) Write a program that...Ch. 11 - (ArrayList of Character) Write a method that...Ch. 11 - (Bin packing using first fit) The bin packing...
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education