Unit 7 Homework (Hacks & Quizizz))
// HACK 1 -
// 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<Boolean> asdf = new ArrayList();
asdf.add(true);
asdf.add(false);
array(asdf);
}
public static void array(ArrayList arr){
if (arr.size()>0){
arr.set(0, "This is an array list");
System.out.println(arr.get(0));
}
}
}
hack1.main(null);
// Hack 2
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");
for(int i = 0; i < color.size(); i++) {
if(color.get(i).contains("red")){
color.remove(i);
}
}
System.out.println(color);
/*/
using
if(color.get(i).contains("red"))
iterate through the arraylist and remove all elements that contain the word red in them
/*/
// 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 a = num.get(0);
int b = num.get(1);
int c = num.get(2);
int sum = a + b + c;
System.out.println(sum);