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.
public class Turtle {
private String name;
public Turtle(String name) {
this.name = name;
}
public static void main(String[] args) {
ArrayList<Boolean> boolArr = new ArrayList<Boolean>();
ArrayList<Turtle> turtles = new ArrayList<Turtle>();
ArrayList<String> strings = new ArrayList<String>();
for (int i = 0; i<10; i++) {
strings.add("xdd");
}
System.out.println(strings.toString());
}
}
Turtle.main(null);
import java.util.ArrayList;
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);
randomNumbers.remove(0);
randomNumbers.add(10);
randomNumbers.set(0, 7);
System.out.println("ArrayList: " + randomNumbers);
}
}
Hack2.main(null);
public class Hack3 {
public static void main(String[] args) {
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++) {
total += values.get(i);
}
System.out.println("total: " + total);
}
}
Hack3.main(null);
int[] arr = {1, 5, 3, 4, 2};
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]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
// Printing sorted array
System.out.print(arr[i] + " ");
}
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
if (i % 2 != 0) {
System.out.print(arr[i] + " ");
}
}
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
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.
import java.util.ArrayList;
public class Seating {
public ArrayList<String> chart;
public Seating() {
chart = new ArrayList<String>();
}
public void add (String name) {
chart.add(name);
}
public void replace (int index, String newName) {
chart.set(index, newName);
}
public void alphabetize() {
int length = chart.size();
for (int i = 0; i<length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (chart.get(i).compareTo(chart.get(j)) > 0) {
String temp = chart.get(j);
chart.set(j, chart.get(i));
chart.set(i, temp);
}
}
}
}
public void print() {
int length = chart.size();
for (int i = 0; i < length; i++) {
System.out.println(chart.get(i) + " ");
}
}
public static void main (String[] args) {
Seating seating = new Seating();
seating.add("Sahil");
seating.add("Samuel");
seating.add("Everitt");
seating.print();
seating.replace(0, "Nathan");
System.out.println();
seating.print();
seating.alphabetize();
System.out.println();
seating.print();
}
}
Seating.main(null);