Python_notes
Python_notes
fdkajfklajskfasl
fdflafdkalflaklfls
ArraysBreak / Continue
Animal [] zoo = new Animal [4];
zoo [0] = new Tiger();
zoo [1] = new Giraffe();
…for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
//This skips the value of 4
}
if (i == 6) {
break;
//This jumps out of the for loop
}
}
String [] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
//Outputs 4
int [][] myNumbers = {{1, 2, 3, 4}, {4, 5, 6}};
int x = myNumbers [1][2];
System.out.println(x);
//Outputs 6
If...Else
Arrays.sort(cars);
System.out.println(Arrays.toString(cars));
//[BMW, Ford, Mazda, Volvo]
SORTING OBJECTS WITH MULTIPLE PARAMETERS:
NATURAL SORTING
(Created within class)
public class Person implements Comparable<Person>{…
@Override
public int compareTo(Person o) {
return Double.compare(this.weight, o2.weight);
}
----> Use wrapperclass
int time = 22;
if (time < 10) {
System.out.println("Good morning!");
} else if (time < 20) {
System.out.println("Good day!");
} else {
System.out.println("Good evening!");
}
//Outputs "Good evening!"
variable = (condition) ? expressionTrue :
Arrays.sort(listOfPeople);
/
Collections.sort(...);
ALTERNATIVE SORTING
(Created in sepparate class)
public class SortOnName implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.getName().compareTo(o2.getName());