Notes

Difference between Arrays and ArrayList

Arrays ArrayLists
Static (fixed size) Dynamic (can change size)
Fundamental java feature Part of a framework. Someone was nice and designed this with the behind the scenes being arrays
An object with no methods A class with many methods
Not as flexible Designed to be more flexible
Can store more primitive data Not designed to store primitives, they store object references
Slightly slower than Arrays
Can only be used with an import statement

Difference between List and ArrayList

List ArrayList
It is an interface It is class
It creates a list of objects that can be accessed by the individual index number It creates a dynamic array that can be expanded when needed
It extends the collection framework. It extends AbstractList class and implements the List interface
It can not be instantiated It can be instantiate

Data Types

Primitive Data Types:

  • boolean
  • char
  • double
  • int

Wrapper Class Data Types (Store the primitive values as objects)

  • Boolean
  • Character
  • Double
  • Integer

ArrayList Methods

size();

  • Returns the number of elements in the list

add(obj);

  • Adds element at the end

add(index, object);

  • Adds element at specific index

remove(index);

  • Removes element from specific index

set(index, object);

  • Replaces element at index with new object

get(index);

  • Returns element at index

use size.() to return length/number of elements instead of .length() use get() instead of bracket notation for getting an element of an arraylist

Common Mistakes

  • don't forget to import java.util.ArrayList
  • don't declare or instantiate ArrayList with a primitive data type, which are things such as int, double, and booleans.
  • don't forget the parentheses at the end of the constructor and the element types with the brackets: ArrayList list = new ArrayList(); </li>
  • don't confuse arrays with arraylists, don't use [], don't use .length use .size() instead
  • </ul> </div> </div> </div>

    Hack 1

    // HACK!!!!
    // Create an arrayList and use one of the cool methods for it
    
    import java.util.ArrayList; 
    
    public class hack1 {
        public static void main (String[] args) {
           ArrayList<String> names = new ArrayList<String>();
           names.add("Linda");
           names.add("Emily");
           names.remove(0);  //remove first item
           System.out.println(names.size()); //.size() = .length(), .length() is for String
           System.out.println(names);
        }
    }
    
    hack1.main(null);
    
    1
    [Emily]
    

    Hack 2

    //hack 2
    
    import java.util.ArrayList;
    
    public class hack2 {
        public static void main (String[] args) {
    ArrayList<String> color = new ArrayList<String>(); 
    color.add("red apple");
    color.add("green box");
    color.add("blue water");
    color.add("red panda");
    
    for (int i=0; i<color.size(); i++){ //use .size() instead of .length()
        if(color.get(i).contains("red")) {
            color.remove(i);
        }
    }
        System.out.println(color);
    
        }
    }
    
    hack2.main(null);
    
    /*/ 
    using 
    
    if(color.get(i).contains("red"))
    
    iterate through the arraylist and remove all elements that contain the word red in them
    /*/
    
    [green box, blue water]
    

    Hack 3

    //hack 3
    // find the sum of the elements in the arraylist
    
    ArrayList<Integer> num = new ArrayList<Integer>(); 
    
    num.add(5);
    num.add(1);
    num.add(3);
    
    int sum = 0; 
    for (int i=0; i<num.size(); i++) { 
        sum += num.get(i); //find sum by adding all items in ArrayList
    }
    
    System.out.println(sum);
    
    9
    

    Screenshot of Quizizz Completed

    unit8

    </div>