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: Brief Version (11th Global Edition)
- 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_forwardWhat criteria should be used to evaluate an ArrayList's performance?arrow_forwardQ: 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_forward
- Consider 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_forward6. Consider the following code segment: ArrayList list = new ArrayList(10) ; list.add( "Ali" ); list.add( "Ahmed" ); list.add( "Hassan" ); list.add( 0, "Qasim" ); Which of the following elements will be at index 1 of the list? K Ali Ahmed Hassan Qasimarrow_forward
- Computer 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_forwardTopic: Linked List SHOW THE OUTPUT ONLYarrow_forwardSubtract each element in origList with the corresponding value in offsetAmount. Print each difference followed by a space. Ex: If the input is: 4 5 10 12 2 4 7 3 the output is: 2 1 3 9 1 #include 2 3 int main(void) { const int NUM_VALS = 4; int origlist[NUM_VALS]; int offsetAmount[NUM_VALS]; int i; 4 6 8 scanf("%d", &origList[0]); scanf("%d", &origList[1]); scanf("%d", &origList[2]); scanf("%d", &origList[3]); 10 11 12 13 scanf("%d", &offsetAmount[0]); scanf ("%d", &offsetAmount[1]); scanf("%d", &offsetAmount[2]); scanf("%d", &offsetAmount[3]); 14 15 16 17 18 19 * Your code goes here */ 20 21 printf("\n"); 22 23 return 0; 24 }arrow_forward
- //give the output of the following program and draw the link list. struct node{ int info; struct node *link;};typedef struct node *nodePTR;void insort(nodePTR*,struct Info);nodePTR getnode();int main(void){nodePTR p=NULL,head=NULL,save;inti; for(i=0;i<3;i++){ insort (&head,i+5);}save=head;do{ printf("%d",save->info); save=save->link;}while(save!=NULL); return0;}nodePTR getnode(){nodePTR q;q=(nodePTR)malloc(sizeof(struct node));return q;}void insort(NODEPTR *head , int x){ NODEPTR p,q,r; q=NULL; for(p=*head; p!=NULL &&x>p->info; p=p->link) q=p; if(q=NULL) { p=getnode(); p->info=x; p->link=head; head=p; }else{ r=getnode(); q->link=r; r->info=x; r->link=p; }}arrow_forwardThe following array of structures is used to hold data of your IPC144 grade center strcut grades {char name[101]; unsigned final; unsigned total;} struct grades myClass[25]; Write a function that gets the class grades array as a parameter and prints the list of people who passed the course (a student passes a course if he/she passes the final as well as the total) and their total mark. The function should also print the class average at the end (based on the total marks) void printPassedAverage(struct grades myClass, int size) The output should be like this Students passed:--------------------- John Smith 85 Jane Doe 65 Roy Crowe 80 Julia Stuart 55 Rob Gates 60 Class average: 69arrow_forwardEach true or false is a subpart to the same question. Thanks. True or False 1. [ {int id1; id2 = id1 + 5; } ] < [ {int x; x = y+ 5; } ] 2. [ {type, id1; id, = id1 + 5; } ] <[ {int x; y= x+5; } ] True or False 1. [ stmt1; {stmt list1}; id2 = id1 + 5; ] « [ int x; {x = 2; }; x = x + 5; ] 2. [ int id1; {stmt_list}; stmt1; ]«[ int x; {x = 2; }; x = x + 5; ] True or False 1. [ stmt1; stmt2; ] < [ int x; y = 5; ] 2. [ {stmt.list,}]<[ {int x; y = 5; } ] True or False 1. [ {int id1; id2 = E1 + E2; } ] <[ {int x; x= 4+ 5; } ] 2. [ {int id1; id2 = y * T1; } ] < [ {int x; x = y+ 5; } ]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