Chapter 10 - Arrayslist/Java Collections Flashcards
What does the ArrayList class provide?
The basic functionality that comes with a standard array, plut it provides additional functionality
What is the basic functionality of an ArrayList?
Stores an ordered collection of values and allows access to the value via an index.
What is the added functionality of an ArrayList?
Grows and shrinks dynamically by inserting and deleting elements at any specified location.
What do you type to import the ArrayList class?
import java.util.ArrayList;
How do you initialize an ArrayList reference variable?
Use this syntax in a declaration:
ArrayList(Student) variablename = new ArrayList<>();
It should be angled brackets around Student instead of parathesis <>
Comparative Syntax for creating ArrayList objects, regular objects and standard Arrays
What syntax adds and element to the end of an ArrayList?
ArrayList-reference-variable.add(item);
The “item” that’s being added must be the same type as the type specificied in the ArrayList’s declaration.
What does API stand for?
Application Programming Interface
What is the Java API?
The interface to the huge libaray of pre-built Java classes.
What do you have to use to interface with with Java API classes?
Their public methods
What information do you have to know to use a public method?
The type of argument to pass to it, and what type of value it returns.
What does a method’s API show?
The method’s parameters and its return type.
ArrayList objects don’t use square brackets like standard arrays when accessing and updating an element, what do they use instead?
A get method to access the element
A set method to update and element
What does the E return type in the ArrayList’s get method mean?
It stands for element. Represents the data types of the ArrayList’s elements.
It should be the same as the element-type specified in the ArrayList’s initialization:
ArrayList(element-type) reference-variable = new ArrayList<>();
It should be angled brackets around element-type <>
What does the set method allow you to do?
Assign a value to an existing ArrayList.
API heading example:
public E set(int index, E elem)
What does the following Syntax do?
public void add(int index, E elem)
Starting with the specified index position, shift the original elements to higher-indexed positions. Then insert the elem parameter at the specified index position.
What does the following Syntax do?
public void clear()
Removes all elements from the list.
What does the following Syntax do?
public int indexOf(Object elem)
(Object is a generic class that can be used as a class type for any object)
Search for the first occurrence of the elem parameter within the list. If it’s found, return its index position. If it’s not found, return -1.
What does the following syntax do?
public boolean isEmpty()
Return true is the list contains no elements.
What does the following syntax do?
public E remove(int index)
Remove the element at the specified index position, shift all higher-indexed element to lower-indexed positions, and return the removed element.
What does the following syntax do?
public int size()
Return the number of elements in the list.
What is the output if you print of concatenate an ArrayList?
A comma-seperated list of ArrayList elements surrounded by square brackets.