Array List

  • basketball like array - always 6 people
  • dodgeball like arraylist - number of people playing changes
import java.util.ArrayList; 

public class hack1 {
    public static void main (String[] args) {
        ArrayList<String> hetvifavs = new ArrayList<String>(Arrays.asList("ripped jeans", "rap music", "black nail polish"));
        System.out.println( "items " + hetvifavs + " Hetvi's favorite things. There are " + hetvifavs.size() + " things in the list" ); 
        
        //objects you add must be of the same data type
        hetvifavs.add("bleached hair");
        System.out.println( "items " + hetvifavs + " Hetvi's favorite things. There are " + hetvifavs.size() + " things in the list" ); 
    }
}

hack1.main(null);
items[ripped jeans, rap music, black nail polish] Hetvi's favorite things. There are 3 things in the list
items[ripped jeans, rap music, black nail polish, bleached hair] Hetvi's favorite things. There are 4 things in the list

Traversing ArrayLists

  • use for loops
  • use get() instead of bracket notation for getting an element of an arraylist
  • use size() to find number of elements in arraylist instead of using .length
import java.util.ArrayList;

ArrayList<String> color = new ArrayList<String>(); 
color.add("red apple");
color.add("green box");
color.add("blue water");
color.add("red panda");


/*/ 
using 

if(color.get(i).contains("red"))

iterate through the arraylist and remove all elements that contain the word red in them
/*/

for (int i = 0; i<color.size(); i++) {
    if (color.get(i).contains("red")) {
        color.remove(i);
    }
}

System.out.println(color);
[green box, blue water]
ArrayList<Integer> num = new ArrayList<Integer>(); 

num.add(5);
num.add(1);
num.add(3);

int sum = 0;
for (int i : num){
    sum += i;
}
System.out.println(sum);
9

Flagging

  • locate data in linear structure
  • usually use for loops - check one at a time
  • use the == operator or .=

Sorting

  • identify max or min value
  • building a sorted structure
    • while loop as inner loop

Ethical Issues

  • minimize user data collection - remove data no longer needed
  • anonymize data with hash code

Quizzez Homework

This is an image