Unit 6

  • Arrays are a type of data structure that contains a collection of data
    • data in a Java array can be primitive or referenced
  • Element → one value in an array
  • Index → the position of the value in array (most languages, including Java, use 0 indexing)
  • Arrays are objects defined by type[] name = new type[size] or initialized with elements using type[] name = new type[]{a,b,c,...}
  • Traversing an array is accessing the values inside of it
    • Enhanced for loops can iterate through arrays
  • Index goes from 0 to size-1
  • Algorithms can be used to modify or get info about arrays
    • Finding sums of arrays, finding every other element in the array, check for duplicates
  • Uninitialized and Unfilled Arrays - Allocate a single array variable but not the whole array
int[] arraytest = new int[5];
int[] arraytest2 = {2, 4, 21, 6, 11};
System.out.println(arraytest[2]);
0
arraytest[5];
---------------------------------------------------------------------------
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
	at .(#18:1)
for(int i = 0; i < arraytest2.length; i++){
	System.out.println(arraytest2[i]);
}
2
4
21
6
11

Unit 7

  • Wrapper class stores prim in objects
  • make sure to specify the data type
  • return arraylist using the return command
  • traverse arraylist through for loops and while loops
  • get() to get an element
  • size() to get size
  • add(int index, element)
  • addAll(int index, Collection collection)
  • clear()
  • remove(int index)
  • remove(element)
  • get(int index)
  • set(int index, element)
// Importing
import java.util.ArrayList;

// Creating an ArrayList
ArrayList<String> array = new ArrayList<>();
array.add("aa");
array.add("bb");
array.add("dd");
array.add("ii");
System.out.println(array);
[aa, bb, dd, ii]
ArrayList<String> array1 = new ArrayList<>();
array1.add("ee");
array1.add("ff");
array1.add("ff");
array1.add("gg");
array1.add("hh");
System.out.println(array1);
[ee, ff, ff, gg, hh]
// add(int index, element)
array.add(2, "cc");
System.out.println(array);
[aa, bb, cc, dd, ii]
// addAll(int index, Collection collection)
array.addAll(4, array1);
System.out.println(array);
[aa, bb, cc, dd, ee, ff, ff, gg, hh, ii]
// size()
array.size();
10
// clear()
array1.clear();
System.out.println(array1);
[]
// remove(int index)
array.remove(6);
System.out.println(array);
[aa, bb, cc, dd, ee, ff, gg, hh, ii]
// remove(element)
array.remove("ii");
System.out.println(array);
[aa, bb, cc, dd, ee, ff, gg, hh]
// get(int index)
array.get(5);
ff
// set(int index, element)
array.set(5, "dd");
System.out.println(array);
[aa, bb, cc, dd, ee, dd, gg, hh]
// indexOf(element)
array.indexOf("dd");
3
array.indexOf("ff");
-1
// lastIndexOf(element)
array.lastIndexOf("dd");
5
array.lastIndexOf("ii")
-1
// equals(element)
array.equals(array1);
false
ArrayList<String> equal = new ArrayList<>(Arrays.asList("aa", "bb", "cc", "dd", "ee", "dd", "gg", "hh"));
array.equals(equal);
true
// hashCode()
array.hashCode();
-152662207
// isEmpty()
array.isEmpty();
false
array1.isEmpty();
true
// contains(element)
array.contains("bb");
true
array.contains("kk");
false
ArrayList<String> contain = new ArrayList<>(Arrays.asList("bb", "cc", "dd", "ee", "gg"));
// containsAll(Collection collection)
array.containsAll(contain);
true
contain.add("jj");
array.containsAll(contain);
false
// sort(Comparator comp)
array.add("1");
array.add("10000");
array.sort(Comparator.naturalOrder());
System.out.println(array);
[1, 10000, aa, bb, cc, dd, dd, ee, gg, hh]
array.set(4, "9eji");
array.set(8, "z8f");
array.add("9f8");
array.add("y8de1");
array.add("08de1");
array.sort(Comparator.reverseOrder());
System.out.println(array);
[z8f, y8de1, hh, ee, dd, dd, bb, aa, 9f8, 9eji, 10000, 1, 08de1]

Unit 8

  • 2D Array is an array of arrays, and a type of multidimensional array.
  • To create a 2D array, you write the type of the class, followed by the name, and the values.
    • datatype[][] name = new data type[#][#]
  • Traverse 2D arrays through Nested Loops
  • You can change elements or access elements in 2D arrays by using the indexes
  • Index is the position of the element in the array (starts from 0)
  • Array Length is the number of elements in the array
import java.util.Scanner;

public class Arrays {
    int[][] arr = new int[5][5];

    public void setArbitraryArray() {
        for (int i = 0; i<arr.length; i++) {
            for (int j = 0; j<arr[i].length; j++) {
                arr[i][j] = (int) (Math.random()*20)/ (i+2);
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public void reverseArray() {
        for (int i = arr.length-1; i >= 0; i--) {
            for (int j = arr[i].length-1; j >= 0; j--) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    public void findValue() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter Row: ");
        int row = scanner.nextInt();
        System.out.println(row);
        System.out.print("Enter Column: ");
        int col = scanner.nextInt();
        System.out.println(col);
        System.out.println(arr[row][col]);
    }

    public void rowProduct() {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            int prod = 1;
            for (int j = 0; j <arr[i].length; j++) {
                prod = prod * arr[i][j];
            }
            sum += prod;
        }
        System.out.println("Sum of row products: " + sum);
    }

    public static void main(String[] args) {
        Arrays arr = new Arrays();
        System.out.println("Arbitrary Values:");
        arr.setArbitraryArray();
        System.out.println("Reversed Array:");
        arr.reverseArray();
        System.out.println("Value of arr[4][2]:");
        arr.findValue();
        arr.rowProduct();
    }
}
Arrays.main(null);

Unit 9

  • Inheritance is the ability to inherit characteristics from super class to sub classes
    • Reduces risk of error and reduces redundancy
    • extends key word used to inherit characteristics from a class
    • Constructors in a sub class needs to have at least the same definition as the old class
    • super() to call supper class's constructor which must have all relevant parameters passed into it
    • @Override to overriding methods of a super class and makes it readable
  • Tree called inheritance hierarchies where lower subclasses inherit upper super classes in the hierarchy
  • Polymorphism allows methods to have multiple forms for code reusability with different forms/implementations
public class WorldCup{
    private int wins;
    private int losses;
    private int draws;

    public WorldCup(int wins, int losses, int draws){
        this.wins = wins;
        this.losses = losses;
        this.draws = draws;
    }

    public String toString(){
        return ( "{ \"wins\": "  +this.wins+  ", " + "\"losses\": "  +this.losses+  ", " + "\"draws\": "  +this.draws+ " }" );
    }

    public static void main(String[] args) {
        Argentina argentina = new Argentina(5, 1, 0);
        System.out.println("Argentina: " + argentina);

        Croatia croatia = new Croatia(3, 1, 2);
        System.out.println("Croatia: " + croatia);

        France france = new France(4, 1, 0);
        System.out.println("France: " + france);

        Morocco morocco = new Morocco(4, 0, 1);
        System.out.println("Morocco: " + morocco);

        Brazil brazil = new Brazil(3, 2, 0);
        System.out.println("Brazil: " + brazil);
    }
}

public class Argentina extends WorldCup{
    public Argentina(int wins, int losses, int draws){
        super(wins, losses, draws);
    }
}

public class Croatia extends WorldCup{
    public Croatia(int wins, int losses, int draws){
        super(wins, losses, draws);
    }
}

public class France extends WorldCup{
    public France(int wins, int losses, int draws){
        super(wins, losses, draws);
    }
}

public class Morocco extends WorldCup{
    public Morocco(int wins, int losses, int draws){
        super(wins, losses, draws);
    }
}

public class Brazil extends WorldCup{
    public Brazil(int wins, int losses, int draws){
        super(wins, losses, draws);
    }
}
WorldCup.main(null);

Unit 10

  • A recursive method is method that calls itself.
  • It contains at least one base case that halts recursion and once recursive call
  • Each recursive call has own local variables
  • Parameter values take progress of recursive process
  • A recursion can be replaced with an iterative and give the same result
  • Recursion can traverse String, array, and ArrayList objects
  • binary search - sorted in order to maximize efficiency
  • Linear recursion - only calls itself once
  • Selection sort - finds the minimum value
  • Merge sort - divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves
import java.io.*;
class Fibonacci {
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
 
    public static void main(String args[])
    {
        int n = 13;
        System.out.println(fib(n));
    }
}
Fibonacci.main(null)
233