CSIT254 Homework 03 New CollectionClasses-1

docx

School

Raritan Valley Community College *

*We aren’t endorsed by this school

Course

254

Subject

Business

Date

Apr 3, 2024

Type

docx

Pages

2

Uploaded by GeneralSandpiper2555

Report
CSIT 254 – Homework 3 - Ch3 –Collection Classes Name Katerine Gomez 1. What is a collection class? A collection class is a special kind of class designed to contain elements of the same data type. It serves as a data structure for storing and managing a group of related elements. This class facilitates efficient organization and manipulation of data. 2. List and describe 4 common operations for collection classes: 1. Add: This one allows you to add a new element to the collection. if you have a collection of numbers and you want to add a new number to it, we would use the "add" operation. 2. Remove: This one lets us remove an existing element from the collection. If we have a collection of names and you want to remove a particular name from it, we would use the "remove" operation. 3. Size: This one returns the number of elements currently present in the collection. It provides insight into the size or quantity of elements within the collection. For example, to determine how many items are in a collection of shoes, we would use the "size" operation. 4. Exists: This one checks whether a specific element exists in the collection. It returns true if the element is found in the collection and false otherwise. For example, if we have a collection of names and we want to check if the name "Kate" exists in it, we have to use the "exists" operation. 3. Given the line of code below from the enlargeArray( ) method, where data[ ] is an array of int and populated as shown, answer the questions regarding the newArray Code: int[] newArray = new int[data.length*2 + 1]; logical depiction of data[ ] array, before code is encountered: data 18 6 27 5 0 1 2 3 🡨 indexes a. What is the capacity of newArray? 9 b. What is the first index for newArray? 0 c. What is the last potential index for newArray? 8 Submit via Canvas CSIT 254-51x Homework 03 CollectionClasses Fall19.docx
CSIT 254 – Homework 3 - Ch3 –Collection Classes Honors Option Additional Question Of these three variations on determining the size for newArray, what advantages and/or disadvantages do they have compared to each other? fm1 : data.length*2 + 1 fm2 : data.length + 1 fm3 : (data.length + 1)*2 On formula #1 data.length*2 + 1 (doubling the length and then adding 1) gives us extra room for our array to grow without needing to resize too often, which can be helpful to save time. However, it might use more memory than we actually need. Formula #2 data.length + 1 (simply adding 1 to the length) is simple and does not waste much memory, but if the data grows up quickly, we might need to resize the array more frequently. Formula #3 ((data.length + 1)*2) (first adding 1 to the length and then doubling it) tries to balance between extra room and memory usage, but it still might use more memory than formula 2. Submit via Canvas CSIT 254-51x Homework 03 CollectionClasses Fall19.docx
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help