Part 3!

Discovering errors

  • How to debug with comments

  • I didn’t know this bool works this way ```java boolean check = false;

if(!check) // same as if(check == false)


* Difference Between If and Else If: The difference is that if the first if is true, all of the other else ifs won't be executed, even if they do evaluate to true. If they were individual ifs, nevertheless, all of the ifs will be executed if they evaluate to true.

## Lists
* ArrayList<Type> list = new ArrayList<>()

* There are 2 types of variables in Java - value type (primitive) and reference type (reference type). Value-type variables such as int or double hold actual values. Reference-type variables such as ArrayList, in contrast, contain a reference to the location that contains the value(s) relating to that variable. Value-type variables can hold a very limited amount of information, whereas references can store a near limitless amount of it.

* Once a list has been created, ArrayList assumes that all the variables contained in it are reference types. Java automatically converts an int variable into Integer when one is added to a list, and the same occurs when a variable is retrieved from a list. This means that even though a list is defined to contain Integer-type variables, variables of type int can also be added to it.

* .add() takes the value to be added as a parameter and print the value at position zero

* .remove(int index or value you want to remove) removes the value that is located at the index that's given as the integer parameter like remove(1)

* .get() retrieves a value from a certain position

* .size() returns the number of elements the list contains

* .indexOf() gives index of value in parentheses

* .contains() checks the existence of a value in the list. The method receives the value to be searched as its parameter, and it returns a boolean type value (true or false)

```java
ArrayList<String> teachers = new ArrayList<>();

teachers.add("Simon");
teachers.add("Samuel");
teachers.add("Ann");
teachers.add("Anna");

for (int index = 0; index < teachers.size(); index++) {
    System.out.println(teachers.get(index));
}
// prints simon, sam, ann, anna

//reverse
int index = numbers.size() - 1;
while (index >= 0) {
    int number = numbers.get(index);
    System.out.println(number);
    index = index - 1;
}
  • for (TypeOfVariable nameOfVariable: nameOfList), where TypeOfVariable is the list’s element type, and nameOfVariable is the variable that is used to store each value in the list as we go through it. e.g. for(int i:myList)

  • If the parameter given to remove is the same type as the values in the list, but not an integer, (integers are used to remove from a given index), it can be used to remove a value directly from the list like remove(“hola”)

  • If the list contains integers, you cannot remove a number value by giving an int type parameter to the remove method. This would remove the number from the index that the parameter indicates, instead of an element on the list that has the same value as the parameter. To remove an integer type value you can convert the parameter to Integer type; this is achieved by the valueOf method of the Integer class like list.remove(Integer.valueOf(15));

  • List can be used as a parameter to a method too, like variables. When the method is defined to take a list as a parameter, the type of the parameter is defined as the type of the list and the type of the values contained in that list.

public static void print(ArrayList<String> list) {
    for (String value: list) {
        System.out.println(value);
    }
}
ArrayList<String> strings = new ArrayList<>();
print(strings)
  • chosen parameter in the method definition is not dependent on the list that is passed as parameter in the method call. In the program that calls print, the name of the list variable is string, but inside the method print the variable is called list — the name of the variable that stores the list could also be printables, for instance.

  • can also define multiple variables for a method.

public static void printSmallerThan(ArrayList<Integer> numbers, int threshold) {
    for (int number: numbers) {
        if (number < threshold) {
            System.out.println(number);
        }
    }
}

ArrayList<Integer> list = new ArrayList<>();
printSmallerThan(list, 3);
  • Method can return value, without void
public static double average(ArrayList<Integer> numbers) {
if (numbers.size() == 0) {
    return -1.0;
}

int sum = 0;
for (int number: numbers) {
    sum = sum + number;
}

return 1.0 * sum / numbers.size();
}
  • When copying list to method parameter, the method receives the value of the list variable, i.e., a reference. In such a case the method receives a reference to the real value of a reference-type variable, and the method is able to modify the value of the original reference type variable, such as a list. But variables such as int are used as method parameters, the value of the variable is just copied for the method’s use and is not modified.

Arrays

  • Size of the ArrayList is unlimited whereas array contains a limited amount of numbered spots (indices) for values. The length (or size) of an Array is the amount of these spots, i.e. how many values can you place in the Array. The values in an Array are called elements.

  • 1st way to create array is explicitly define the size upon creating. An array is declared by adding square brackets after the type of the elements it contains (typeofelements[]). A new Array is created by calling new followed by the type of the elements, square brackets and the number of the elements in the square brackets.

int[] numbers = new int[3];
numbers[0] = 2;
numbers[2] = 5;
  • 2nd and final way to create array. So apart from calling for new, we can also initialize an array with a block, that contains comma-separated values to be assigned in the array. This works for all the types:
String[] arrayOfStrings = {"Matti L.", "Matti P.", "Matti V."};
double[] arrayOfFloatingpoints = {1.20, 3.14, 100.0, 0.6666666667};
  • Assigning a value to a specific spot of an Array works much like assigning a value in a normal variable, but in the Array you must specify the index, i.e. to which spot you want to assign the value. The index is specified in square brackets.

  • find the size of the array through the associated variable length. Note it is not method so .length() doesn’t work

  • When you use array as a parameter of a method, the method receives a copy of the reference to the array.

Strings

  • Strings can’t be compared with with the equals operator ==. Use .equals() method

  • while (!input.isEmpty()) does not allopw empty inputs

  • .split(“ “) method returns an array of the resulting sub-parts

String text = "first second third fourth";
String[] pieces = text.split(" ");
System.out.println(pieces[0]);
System.out.println(pieces[1]);
System.out.println(pieces[2]);
System.out.println(pieces[3]);

System.out.println();

for (int i = 0; i < pieces.length; i++) {
    System.out.println(pieces[i]);
}
  • .contains() method -> Strings have a contains-method, which tells if a string contains another string

  • .chartAt() gets character as specified index of string

  • Splitting a string always produces an array of strings. So if the text is of fixed format, we can assume the data in a specific index to always be of the a specific type and can use Integer.valueOf() to convert string to number for further calculations like sum

  • .length() method exists for strings (rmb .length is not a method for array)

while (true) {
    String input = scanner.nextLine();
    if (input.equals(" ")) {
        break;
    }

    String[] pieces = input.split(",");
    int age =0;
    if(Integer.parseInt(pieces[1])>age){
        age = Integer.parseInt(pieces[1]);
    }
    System.out.println("Age of the oldes: " + age);
}