Exam 2 Flashcards
An array is an indexed structure:
elements may be accessed in any order using subscript
elements can be accessed in sequence using a loop that increments the subscript
With Java arrays object , you cannot
increase or decrease its length (length is fixed )
insert an element at a specified position
remove an element at a specified position
A _____ is a collection of elements, each with a position
list
In Java, the methods to be implemented by lists are defined in the ____ interface
List
Classes that implement the List interface provide the functionality of an array and offer several operations :
Obtain an element at a specified position
Replace an element at a specified position
Find a specified target value
Add an element at either end a
Remove an element from either end
Insert or remove an element at any position
Traverse the list
The simplest class that implements the List interface
ArrayList class
Use an ArrayList class when:
you will be adding new elements to the end of a list
you need to access elements quickly in arbitrary order
How do you declare a List “object” whose elements will reference String objects :
List myList = new ArrayList();
ArrayList is empty and has a default capacity of:
10 elements
In an ArrayList, you cannot access an element using a bracket index as you can with arrays (array[1]).
Instead use _____ method.
get()
How to search an ArrayList to get location
.indexOf()
List myList new ArrayList <>() ; language feature called:
generics
The statement creates a List of Strings; only references of type String can be stored in the list
Generic collections
In generic collection, String in this statement is called a:
type parameter
The _______________ sets the data type of all objects stored in a collection
type parameter
The general declaration for generic collection is:
CollectionClass ab1e= new 1ass<>() ;
In a generic collection The indicates a:
type parameter
Adding a noncompatible type to a generic collection will generate an error during:
compile time
The elements of a list can be of any type, including your own classes :
List inventory new ArrayList (); = new ArrayList();
In order to use _______ with a List of user-defined objects, there must be a way to determine when two objects are equal
indexof
indexOf is done by overriding the ______ method inherited from ______
equals
object
The equals method has a parameter of type:
Object
How to use Object.equals
public boolean equals (Object other)
What does Object.equals do?
Compares two references to determine if they refer to the same object:
An Object.equals method class must _____________ for the comparison to be meaningful
Override equals
How do you override an equals() in computer class?
@Override public boolean equals (Object obj) { if (obj instanceof Computer) { Computer other = (Computer) obj: return computePower() == other.computePower } (); return false ; }
Internally, the elements of an ArrayList are stored in an array. Three variables are needed:
theData : An array to hold the elements capacity : Physical size of array
size : Number of data items in the list