Unit 7 - ArrayList Flashcards
ArrayList object
Mutable and contains object references Dynamic in size Resizable length Not designed to store primitives Slightly slower
ArrayList can only be used when
imported
ArrayList class is implemented using
Arrays
framework
prewritten, high-performing and efficient code that can handle and use objects of groups of data
import statement
import.java.util.ArrayList
Declaring a variable to reference an ArrayList object
ArrayList variableName;
Instantiate an ArrayList object
Stores only elements of the same, non-primitive DataType
new ArrayList (); OR new ArrayList (n); n= initial # of elements
DataType must equal
any non-primitive data type
Although ArrayList objects are designed to store references to objects, using
Wrapper class allows primitive values to be stored in ArrayList
What ArrayList method returns the number of element in the list?
int size ( )
What ArrayList method appends obj to end of list and returns true?
boolean (E obj)
ducks. add(Duck1);
* adding Duck1 to the end of an array named ducks
What ArrayList method inserts obj at position index, moving elements at position index and higher to the right and adds 1 to the list size?
void add (int index, E obj)
symbols. add(3, flowers)
* adding “flowers” to index 3 of an array named symbols, moving all original items at 3 or above 1 index higher and adding 1 to ArrayList
What ArrayList method removes element from position index, moving elements at position index and higher to the left and subtracts 1 to the list size while returning the elements formerly stored at position index?
E remove (int index)
Star s1 = stars. remove(2);
*removing element at index 2 in an array named stars & storing it as s1
System.out.print(s1);
*former element will be printed
What ArrayList method replaces element at position index with obj and returns element formerly at position index?
E set (int index, E obj)
Stars s1 = stars.set(1, green star)
*setting element at index 1 in an array named stars & storing it as s1
System.out.print(s1);
*former element will be printed
What ArrayList method returns element at position index in list?
E get (int index)
Star s2 = stars.get(0);
ArrayList is preferred over ArrayList because
It allows compiler to find errors that would otherwise be found at run-time
Java allows the generic ArrayList when the generic type E
specifies the type of elements
When ArrayList is specified,
types of references parameters and return type when using the methods are type E
When passed as a parameter in a method, ArrayList are passed as
References to address and not copies of their values
When a method updates elements of a passed ArrayList, the ArrayList’s elements are
Updated automatically
If a method with an ArrayList as a parameter only involves accessing elements of ArrayList without assigning or updating the elements from the ArrayList, then
the myMethod method signature is sufficient
myMethod(questions);
connects to method used with array name inside parentheses
If the method involves adding, updating, or storing elements of the passed ArrayList, then
you need to specify the data type of elements stored in ArrayList
myMethod1(questions);
*call to method
public static void myMethod1(ArrayList arr) {}
*method being called
In order to return an ArrayList, it is preferred that you
specify the type of data of elements that the ArrayList stores
Algorithm for summing all elements in an ArrayList
1 - create an int sum and set to 0
2 - length of element at index i will be added to sum
3 - loop will repeat until all elements has been traversed
4- display sum
Algorithm for removing elements in an ArrayList
Needs to start on the right end of an ArrayList and move down in order to captures all specified element
Structure of enhanced for loop for ArrayList
for (DataType item: nameOfArrayList) { //statement one //statement two // ... }
In enhanced for loops with ArrayList,
No index is needed so no set() or get()
Enhanced for loops make a copy of each entry
During the iterations of an enhanced for loop, ArrayList elements
Can’t be modified, remove from, or added to ArrayList
Common Mistakes with ArrayList
1- Forgetting to include import statement
2- Declaring or instantiating an ArrayList with primitive data type
ArrayList myList = new ArrayList ();
3- Forgetting to include the () at end of the ArrayList constructor call
ArrayList myList = new ArrayList ;
4- Not specifying the element type that the ArrayList references
ArrayList myList = new ArrayList();
5- Trying to update ArrayList values while using an enhanced for loop
6- Changing size of an ArrayList while traversing with an enhanced for loop
7- Removing an element from an ArrayList at the wrong time
Searching
Process incorporates iteration and selection
Specify each element, one at a time, and do not need to track index after execution
Compare value retrieved with value being searched for
Linear search algorithms
Best used when we don’t know any idea about order of data
Look at each element to determine if what we are looking for is in fact inside the array/ ArrayList
Different data types requires
Different comparisons
int values -> == operator
double values -> close enough(math)
object instance -> .equals(otherThing)
Selection sort
Identifies either max/min of compared values and iterates over structure checking if item store matches condition
If so, replaces value stored with that value
Implementation requires helper method to perform swap operation
Insertion sort
Building a sorted structure as it proceed
Inserts each value it finds at appropriate location in data structure using a while loop as inner loop
Efficiency
One of main way to compare programs
How many times does a particular statement get executed?
When collecting data about users, try to
Remove data collected
Minimize data collection
Anonymizing personal data