Arrays, 2D Arrays, and ArrayLists
CB Units 6, 7, 8
If you'd like, you can follow along with the lessons at this link.
Clone this repo (git clone https://github.com/Rebecca-123/rmr-tri3
) and work on the hacks below. You can transfer these hacks to your personal fastpages once you're finished.
import java.util.ArrayList;
// create ArrayLists that satisfy the following
// a) that stores Boolean values
ArrayList<Boolean> boolarray = new ArrayList<Boolean>(3);
boolarray.add(true);
boolarray.add(false);
boolarray.add(true);
System.out.println(boolarray);
// b) that stores Turtle Objects
ArrayList<String> turtlearray = new ArrayList<String>(2);
// c) that initializes with 10 Strings
ArrayList<String> stringarray = new ArrayList<String>(10);
for (int i = 0; i<10; i++) {
stringarray.add("string " + i);
}
System.out.println(stringarray);
import java.util.ArrayList;
import java.lang.Math;
public class Hack2 {
public static void main(Integer[] args) {
ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
randomNumbers.add(1);
randomNumbers.add(4);
randomNumbers.add(7);
randomNumbers.add(12);
randomNumbers.add(23);
System.out.println("ArrayList: " + randomNumbers);
ArrayList<Integer> a = randomNumbers;
int c = (int)(Math.random()*10);
a.set(0, c);
int d = (int)(Math.random()*10);
a.set(4, d);
System.out.println(randomNumbers);
;
;
}
}
Hack2.main(null);
public class Hack3 {
public static void main(ArrayList<Integer> values) {
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(1);
values.add(4);
values.add(7);
values.add(12);
values.add(23);
System.out.println("ArrayList: " + values);
int total = 0;
for (int i=0; i < values.size(); i++) {
;
}
}
}
Hack3.main(null);
for (int i = 0; i < arr.length; i++) {
// nested loop 1 index ahead
for (int j = i + 1; j < arr.length; j++) {
// comparing elements
int temp = 0;
if (arr[j] < arr[i]) {
// write the swap code!
;
}
}
// Printing sorted array
System.out.print(arr[i] + " ");
}
// code here
// code here
public class Test1
{
public static void main(String[] args)
{
int[][] array = { {1,2,3},{-1,-2,-3},{4,5,6} };
//ADD CODE HERE
}
}
// code here
public boolean double23(int[] nums) {
}
// code here
Homework!
Write a seating chart program for your group. Meet the following requirements:
- A 2D array, array, or ArrayList
- A minimum of 3 methods to manipulate the seating chart (ex. alphabetize, shuffle seats, add/replace/delete people)
- Print results
You will be graded on:
- Completion of at least 5 of the above hacks (you can complete these during the in-class activity)
- Completion of the homework
- Extra credit if you complete a College Board FRQ involving arrays/ArrayLists
Complete the hacks/homework on your personal fastpages.