Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java arrays have a significant shortcoming. They cannot be resized after they have been instantiated. This can be a major problem, since you often don't

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Java arrays have a significant shortcoming. They cannot be resized after they have been instantiated. This can be a major problem, since you often don't know how many elements a list of items will need when creating it One strategy is to start out with a small number of elements, then create a larger array when more space is needed, and copy the old array into the new one. This can be a hassle, and complicates the code, however. ArrayList performs this operation automatically, hiding the details inside a class, freeing up valuable "programmer cycles" for more important tasks Another drawback with arrays is that in many cases you have to search for the element that you want. HashMap provides a way to store and look up objects using a "key" value. For example, you might want to have a list of student records, and be able to look up a student by name. HashMap gives you this functionality rrayLists Array List is a " generic", also called a "parameterized type" This means that you can declare different kinds of Array List to hold different object types. Declaring a generic in Java is done with the angle bracket notation For example, The following line of code creates a ArrayList of Strings: ArrayList stringList new ArrayList There are a couple of things to notice here. One is that an ArrayList declaration has two types (1) ArrayList itself, and (2) the kind of objects that the ArrayList will hold. Also, the right side of the statement requires both angle brackets, and parentheses (). We can think of ArrayList String as the type of object we are creating Just like any other object, to instantiate it, we call the constructor using "new" and parentheses 0. Unlike an array, we don't have to give the ArrayList an initial size. I will expand as needed. However, you can declare an initial capacity by passing a parameter to the constructor. The following creates an ArrayList of strings with an initial capacity of 100 ArrayList stringList ArrayList(100); - new Write the answers to the numbered questions (Q1...) in a file called lab7answers.txt. At the top of this file, put our name and section number. Q1) What code would you use to create an ArrayList called studentList that holds objects of type Student? To eliminate some of the redundancy of this notation, Java allows you to omit the "String" inside the angle brackets on the right hand side (called type inference), so the following is equivalent to the above statement ArrayList stringList ArrayList(100); - new Notice that the angle brackets are still required. To add an element to the list, use the method "add

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students also viewed these Databases questions

Question

=+8.8. Suppose that S = (0,1,2 ,... ], Poo =1, and f,o>0 for all i.

Answered: 1 week ago