
Concept explainers
The purpose of this exercise is to create an Array List data structure that mimics the behavior of the Java Standard Library Version.
Task Check List
- ONLY "for" loops should be used within the data structure class.
- The names of identifiers MUST match the names listed in the description below.
data fields: The fields to declare have private accessibility.
- size : stores the number of occupied locations in internal array, and is of type int.
- data : is a reference variable for the internal array and is of type E[ ].
constant: Use to describe defaults.
- DEFAULT_CAPACITY is a constant that holds a default capacity of ten (10) for the underlying array, is of type int , public accessibility and modified to static.
constructors: The overloaded constructors will initialize the data fields size and data.
- The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.
public ArrayList()
- The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException.
@SuppressWarnings("unchecked")
public ArrayList(int capacity)
Note: Java does not allow you to construct a generic array i.e
data = new E[capacity]; // illegal
so, you must use
data = (E[])new Object[capacity]; // valid
generic type: Allows the uses of different data types.
methods: manages the behavior of the internal array.
Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.
Method |
Description |
Header |
add(item) | appends the item specified to the end of the list and updates the number of elements one at a time. This method returns true, if the data was added successfully. |
public boolean add(E item) |
add(index, item) | inserts the item specified at the given index in the list. Shifts subsequent elements to the right and updates the number of elements in list one at a time. |
public void add(int index, E item) |
checkIndex(index) |
checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. Validation means that you cannot access indexes where elements have not been placed. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index. |
private void checkIndex(int index) |
clear() |
clears list of all elements for garbage collection, returns size back to zero. |
public void clear() |
contains(item) | searches for a specific value within the internal array and returns true, if the value is in the list. |
public boolean contains(E item) |
ensureCapacity( targetCapacity) | doubles the capacity of the underlying array, or if the targetCapacity is greater that double the capacity increases the array to the targetCapacity to ensure that it can hold the number of elements specified by the targetCapacity value. This will most likely have a cast to a generic type. |
@SuppressWarnings("unchecked") public void ensureCapacity(int targetCapacity) |
get(index) | returns the item at the specified position in the list. This method first checks if the index requested is valid. |
public E get(int index) |
indexOf(item) | searches for a specific item within the linked structure and returns the first occurrence (i.e. index location) in the array if found, otherwise returns -1 to indicate that the item was NOT FOUND. |
public int indexOf(E item) |
isEmpty() |
returns true, if the list is empty, i.e., the list contains no elements. |
public boolean isEmpty() |
remove(index) | removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. This method first checks if the index requested is valid and reduces the number of elements by one. |
public E remove(int index) |
remove(item) | removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. Reduces the number of elements by one. |
public boolean remove(E item) |
set(index, item) | replaces the item at the specified position with the one specified. This method validates the index before replacing the item. |
public E set(int index, E item) |
shiftLeft(index) |
helper method that shifts elements of internal array left to index location. Does not update the size of the list. |
private void shiftLeft(int index) |
shiftRight(index) |
helper method that shifts elements of internal array right from index location. Does not update the size of the list. |
private void shiftRight(int index)
|
size() | returns the number of elements in the list. This is the size of the occupied locations of the array. |
public int size() |
toString() | displays the contents of the list. |
public String toString() |

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- What is dynamic-routing mobility different from session location mobility?arrow_forwardFleet class: Instance Variables An array that stores Aircraft that represents Delta Airlines entire fleet A variable that represents the count for the number of aircraft in the fleet Methods: o Constructor-One constructor that instantiates the array and sets the count to aero readFile()-This method accepts a string that represents the name of the file to be read. It will then read the file. Once the data is read, it should create an aircraft and then pass the vehicle to the addAircraft method. It should not allow any duplication of records. Be sure to handle all exceptions. o writeFile()-This method accepts a string that represents the name of the file to be written to and then writes the contents of the array to a file. This method should call a sort method to sort the amay before writing to it. sortArray()-This method returns a sorted array. The array is sorted by registration number.u addAircraft- This method accepts an aircraft and adds it to the fleet(the array only if it is not…arrow_forwardThe Java language definition demands that all references to array elements be checked to ensure that the index or indices are in their legal ranges, is an example of VS ____. (one word answers) Blank # 1 Blank # 2 A A/arrow_forward
- In visual basic Create an array that will hold 10 integer values.arrow_forward10. Lottery Application Write a Lottery class that simulates a lottery. The class should have an array of five integers named lotteryNumbers. The constructor should use the Random class (from the Java API) to generate a random number in the range of 0 through 9 for each element in the array. The class should also have a method that accepts an array of five integers that represent a per- son's lottery picks. The method is to compare the corresponding elements in the two arrays and return the number of digits that match. For example, the following shows the lotteryNumbers array and the user's array with sample numbers stored in each. There are two matching digits (elements 2 and 4). lotteryNumbers array: User's array: 7 4 4 2 1 3 7 3 In addition, the class should have a method that returns a copy of the lotteryNumbers array. Demonstrate the class in a program that asks the user to enter five numbers. The program should display the number of digits that match the randomly generated…arrow_forwardOpengl Help Programming Language: c++ I need help setting coordinate boundries for this program so the shape can't leave the Opengl window. The shape needs to stay visiable.arrow_forward
- Write the statement that declares and instantiates an array of type double using the identifier temperatures that will hold 30 values. Edit View Insert Format Tools Table 12pt v Paragraph v :arrow_forwardC++ Visual Studio 2019 Complete #7 Customer Accounts and #8 Search Function for Customer Accounts Program. Create the array with only 2 elements. 7.Customer Accounts Write a program that uses a structure to store the following data about a customer account: Name Address City, State, and ZIP Telephone Number Account Balance Date of Last Payment The program should use an array of at least 10 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Input Validation: When the data for a new account is entered, be sure the user enters data for all the fields. No negative account balances should be entered. 8. Search Function for Customer Accounts Program Add a function to Programming Challenge 7 (Customer Accounts) that allows the user to search the structure array for a particular customer's account. It should accept part of the customer's name as an…arrow_forwardIn visual basic Create and initialize an array that will hold four popular cities in Texas.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





