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