Arrays, 2D Arrays, and ArrayLists
CB Units 6, 7, 8
interface Turtle {
}
ArrayList<Boolean> booleans = new ArrayList<Boolean>();
ArrayList<Turtle> turtles = new ArrayList<Turtle>();
ArrayList<String> strings = new ArrayList<String>(10);
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.add(2);
randomNumbers.remove(1);
randomNumbers.set(0,2);
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.print(total);
}
}
Hack3.main(null);
int [] arr = {5, 6, 24, 8, 9, 19, 44, 3};
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, 3, 5, 7, 9, 11, 13, 15};
for(int i = 0; i < arr.length; i+=2){
System.out.print(arr[i]+ " ");
}
System.out.println();
for(int i = 1; i < arr.length; i+=2){
System.out.print(arr[i]+ " ");
}
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
import java.util.Arrays;
import java.util.Random;
public class SeatingChart {
private String[][] chart;
public SeatingChart(String[][] chart) {
this.chart = chart;
}
public void alphabetize() {
Arrays.sort(chart, (a, b) -> a[0].compareToIgnoreCase(b[0]));
}
public void shuffleSeats() {
Random rand = new Random();
for (int i = chart.length - 1; i > 0; i--) {
for (int j = chart[i].length - 1; j > 0; j--) {
int randomRow = rand.nextInt(i + 1);
int randomCol = rand.nextInt(j + 1);
String temp = chart[i][j];
chart[i][j] = chart[randomRow][randomCol];
chart[randomRow][randomCol] = temp;
}
}
}
public void addPerson(int row, int col, String name) {
chart[row][col] = name;
}
public void deletePerson(int row, int col) {
chart[row][col] = null;
}
public void printChart() {
for (int i = 0; i < chart.length; i++) {
for (int j = 0; j < chart[i].length; j++) {
System.out.print(chart[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
String[][] chart = {{"Iris", "Sahil", "Ellen"}, {"Nathan", "Rohit", "Jun"}};
SeatingChart seatingChart = new SeatingChart(chart);
System.out.println("Original seating chart:");
seatingChart.printChart();
System.out.println("Alphabetized seating chart:");
seatingChart.alphabetize();
seatingChart.printChart();
System.out.println("Shuffled seating chart:");
seatingChart.shuffleSeats();
seatingChart.printChart();
System.out.println("Added person to row 0, col 2:");
seatingChart.addPerson(0, 2, "Grace");
seatingChart.printChart();
}
}
SeatingChart.main(null);
// Part A
public WordPairList(String [] words){
this.allPairs = new ArrayList<WordPair>();
for(int i = 0; i < words.length -1; i++) {
for(int j = i+1; j < words.length; j++) {
WordPair newPair = new WordPair(words[i], words[j]);
this.allPairs.add(newPair);
}
}
}
// Part B
public int numMatches() {
int matches = 0;
for(int i = 0; i < this.allPairs.size(); i++) {
WordPair pair = this.allPairs.get(i);
if(pair.getFirst().equals(pair.getSecond())) {
matches ++;
}
}
return matches;
}