Unit 7 Arrays List
student led lesson notes + homework
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);
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);
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);
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