Concept explainers
Program:
//package to use clone() method
package language;
//import cloneable interface
import java.lang.Cloneable;
import java.lang.String;
import java.lang.System;
//class definition
class ArrayList implements Cloneable
{
// main method declaration
public static void main(String args[])
{
// create new array
ArrayList<String> list = new ArrayList<>();
// add elements into the array
list.add("New York");
// create new ArrayList
ArrayList<String> list1 = list;
// using cloning create another array
ArrayList<String> list2 = (ArrayList<String>) (list.clone());
// add elements
list.add("Atlanta");
// compare the three array elements
System.out.println(list == list1);
System.out.println(list == list2);
//Display the list
System.out.println("list is " + list);
//Display the list1
System.out.println("list1 is " + list1);
//Call get() method and then display the list2
System.out.println("list2.get(0) is " + list2.get(0));
//Call size() method and then display the list2
System.out.println("list2.size() is " + list2.size());
}
}
Want to see the full answer?
Check out a sample textbook solutionChapter 13 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- #include using namespace std; struct ListNode { string data; ListNode *next; }; int main() { ListNode *p, *list; list = new ListNode; list->data = "New York"; p new ListNode; p->data = "Boston"; list->next = p; p->next = new ListNode; p->next->data = "Houston"; p->next->next = nullptr; // new code goes here Which of the following code correctly deletes the node with value "Boston" from the list when added at point of insertion indicated above? O list->next = p; delete p; O p = list->next; %3D list->next = p->next; delete p; p = list->next; list = p->next; delete p; O None of these O p = list->next; %3D list->next = p; %3D delete p;arrow_forwardArrayList<Integer> list = new ArrayList<Integer>(25); What is the type of the ArrayList defined in question number?arrow_forwardPart 1: Add the code of the ArrayListType to your code. Part 2: Create an Ordered_List class derived from the ArrayListType then add the following function to this class: Constructor to create an array of the size specified by the parameter size. The default array size is 20. Sorted Insert this function takes an integer values then add it to the list this function should check whether the value exist in the list or not. If it is already exist then it will not be inserted to the list, this function should keep the list ordered. Remove function to remove an item from the list. The function takes a single parameter that specifies the number to be removed. • Sequential Search a function to search the list for an integer if exist return its location otherwise return -1. This function specifies the number we are looking for as a parameter. Part 3: Implement the following non-member functions to deal with the Ordered List objects: Load file: this function should read a text file consist an…arrow_forward
- Part 1: Add the code of the ArrayListType to your code. Part 2: Create an Ordered_List class derived from the ArrayListType then add the following function to this class: Constructor to create an array of the size specified by the parameter size. The default array size is 20. Sorted Insert this function takes an integer values then add it to the list this function should check whether the value exist in the list or not. If it is already exist then it will not be inserted to the list, this function should keep the list ordered. Remove function to remove an item from the list. The function takes a single parameter that specifies the number to be removed. • Sequential Search a function to search the list for an integer if exist return its location otherwise return -1. This function specifies the number we are looking for as a parameter. Part 3: Implement the following non-member functions to deal with the Ordered List objects: Load file: this function should read a text file consist an…arrow_forwardFunction 1: draw_subregion Complete the implementation of draw_subregion. This function has the following parameters: my_turtle: A turtle object (which will do the drawing) polygon_points: A list of (x, y) points (i.e. a list of tuples) that defines the points of a polygon. This function should make the turtle draw the polygon given by the points. Make sure that you lift your pen before heading to the first point. You should also make sure you return to the very first point at the end (i.e. you will go to the first point in the list two times: once at the beginning, and once at the end). Language Pythonarrow_forwardWhat criteria should be used to evaluate an ArrayList's performance?arrow_forward
- Q: Convert this to sorted array #include<iostream> #include"Student.cpp" class StudentList { private: struct ListNode { Student astudent; ListNode *next; }; ListNode *head; public: StudentList(); ~StudentList(); int IsEmpty(); void Add(Student newstudent); void Remove(); void DisplayList(); }; StudentList::StudentList() { head=NULL; }; StudentList::~StudentList() { cout <<"\nDestructing the objects..\n"; while(IsEmpty()!=0) Remove(); if(IsEmpty()==0) cout <<"All students have been deleted from a list\n"; }; int StudentList::IsEmpty() { if(head==NULL) return 0; else return 1; }; void StudentList::Add(Student newstudent) { ListNode *newPtr=new ListNode; if(newPtr==NULL) cout <<"Cannot allocate memory"; else { newPtr->astudent=newstudent; newPtr->next=head; head=newPtr; } }; void StudentList::Remove() { if(IsEmpty()==0) cout <<"List empty on remove"; else { ListNode *temp=head;…arrow_forwardConsider the code below. #include using namespace std; struct ListNode { string data; ListNode *next; }; int main() { ListNode *ptr, *list; list = new ListNode; list->data = "Boston"; list->next = new ListNode; %D list->next->data = "Houston"; list->next->next = nullptr; ptr = new ListNode; ptr->data = "New York"; ptr->next = list; list = ptr; %3D // new code goes here Copyright 2016-2021 by A.Berrached-- All Rights Reserved. Write C++ code to create and insert a new node with data field "Miami" after the node with data filed "Boston". Edit Format Tablearrow_forwardFor any element in keysList with a value greater than 50, print the corresponding value in itemsList, followed by a comma (no spaces). Ex: If the input is: 32 105 101 35 10 20 30 40 the output is: 20,30, 1 #include 2 3 int main(void) { const int SIZE_LIST = 4; int keysList[SIZE_LIST]; int itemsList[SIZE_LIST]; int i; 4 6 7 8 scanf("%d", &keysList[0]); scanf ("%d", &keysList[1]); scanf("%d", &keysList[2]); scanf("%d", &keysList[3]); 10 11 12 13 scanf ("%d", &itemsList[0]); scanf ("%d", &itemsList[1]); scanf("%d", &itemsList[2]); scanf ("%d", &itemsList[3]); 14 15 16 17 18 19 /* Your code goes here */ 20 21 printf("\n"); 22 23 return 0; 24 }arrow_forward
- Question 6 The default capacity of ArrayList object is: 10 50 Capacity must be specified by the user. 100arrow_forwardComputer Science Part 2: Client Program with ArrayList Create a second client program. This program will contain an ArrayList of StudentGrades objects. The program will perform the following tasks: Create an instance of an ArrayList that will store three StudentGrades objects. Each element in the ArrayList represents student grades for a test (assessment). This means that each StudentGrades object stored by the ArrayList element represents the grades achieved by students for a single test. Prompt the user for the number of students in the class. The prompt text is: Enter the number of students in the course: [Keyboard Input] Add three new StudentGrades objects to the ArrayList. The StudentGrades objects should be populated with random values from 0 to 100. Use the enhanced for loop to print each of the StudentGrades objects to the console. Print a line containing the following text: “LAST ELEMENT BEFORE REMOVE:”. Print the value of the final element in the ArrayList under the heading.…arrow_forward#include <stdio.h>#include <stdlib.h>#include <time.h> struct treeNode { struct treeNode* leftPtr; int data; struct treeNode* rightPtr;}; typedef struct treeNode TreeNode;typedef TreeNode* TreeNodePtr; void insertNode(TreeNodePtr* treePtr, int value);void inOrder(TreeNodePtr treePtr);void preOrder(TreeNodePtr treePtr);void postOrder(TreeNodePtr treePtr); int main(void) { TreeNodePtr rootPtr = NULL; srand(time(NULL)); puts("The numbers being placed in the tree are:"); for (unsigned int i = 1; i <= 10; ++i) { int item = rand() % 15; printf("%3d", item); insertNode(&rootPtr, item); } puts("\n\nThe preOrder traversal is: "); preOrder(rootPtr); puts("\n\nThe inOrder traversal is: "); inOrder(rootPtr); puts("\n\nThe postOrder traversal is: "); postOrder(rootPtr);} void insertNode(TreeNodePtr* treePtr, int value) { if (*treePtr == NULL) { *treePtr = malloc(sizeof(TreeNode)); if…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education