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);