switch (Ausdruck) {
case Wert1:
Anweisungen;
break;
case Wert2:
Anweisungen;
break;
default:
Anweisungen;
}
car extends vehicle
@Override
drive(){}
Vehicle v = new Car();
//calls drive of Car not Vehicle
v.drive();
public boolean groupSum(int start, int[] nums, int target) {
if (start >= nums.length) return (target == 0);
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum(start + 1, nums, target)) return true;
return false;}
Collection<String> validParentheses(int nofPairs){
Collection<String> list = new ArrayList<>();
if (nofPairs == 0) {list.add("");} else {
for (int k = 0; k < nofPairs; k++) {
Collection<String> infixes = validParentheses(k);
Collection<String> suffixes = validParentheses(nofPairs - k - 1);
for (String infix : infixes) {
for (String suffix : suffixes) {
list.add("(" + infix + ")" + suffix);}}}}
return list;}
|
allg. Form
|
|
class Pair<T,U>
|
|
TypeBound (Bedingungen für Type)
|
|
class Node<T extends Comparable<T>>
Node<Person>//ok Node<Student>//Error |
|
Multiple TypeBounds (mit & anhängen)
|
|
class Node<T extends Person & Comparable<Person>>
|
|
generische Methode
|
|
public <T extends Person> T set (T element){...}
<T>: Bedingungen für Input T : Rückgabetyp |
|
Wildcard-Typ (Nutzen: Typargument nicht relevant)
|
|
Node<?> undef --> kein Lesen & schreiben ausser: Object o = undef.getValue();
|
(p1, p2) -> p1.getAge() – p2.getAge() |
p -> p.getAge() >= 18 |
people.sort((p1, p2) -> p1.getName().compareTo(p2.getName())); |
people.sort(Comparator.comparing(Person::getLastName).thenComparing(Person::getFirstName)); |
Utils.removeAll(people, person -> person.getAge() < 18); |
people.sort(Comparator.comparing(p1 -> p1.getLastName().length())); |
people
.stream()
.filter(p -> p.getAge() >= 18)
.map(p -> p.getLastName())
.sorted()
.forEach(System.out::println);
people.stream().mapToInt(p -> p.getAge())
.average().ifPresent(System.out::println);
Map<String, Integer> totalAgeByCity =
people.stream()
.collect(
Collectors.groupingBy(Person::getCity,
Collectors.summingInt(Person::getAge)));
IntStream.range(1, 100)Random random = new Random(); Stream.generate(random::nextInt).limit(100)
private static void reverteText() throws FileNotFoundException, IOException{
try (FileReader reader = new FileReader("input.txt");
FileWriter writer = new FileWriter("outpur.txt")){
int value = reader.read();
String text = "";
while (value >=0){
text = (char)value + text;
value = reader.read();}
writer.write(text);}}
|
public
|
alle Klassen
|
|
protected
|
Package und Sub-Klassen
|
|
private
|
nur innerhalb Klasse
|
|
(keines)
|
innerhalb Package
|
|
byte
|
8 bit (27 bis 27-1)
|
|
short
|
16 bit
|
|
int
|
32 bit
|
|
long
|
64 bit (1L)
|
|
float
|
32 bit (0.1f)
|
|
double
|
64 bit
|
|
+,-,++,--,! (unär)
|
|
*, /, %
|
|
+, - (binär)
|
|
<, <=, >, >=
|
|
==, !=
|
|
&&
|
|
||
|
|
0.1+0.1+0.1 != 0.3
|
|
|
Problem: double/float
|
x == y |
|
Lösung
|
Math.abs(x – y) < 1e-6 |
|
binär
|
0b10 = 2
|
|
oktal
|
010 = 8
|
|
hex
|
0x10 = 16
|
|
opt.
|
1000 = 1_000
|
1 / 0 --> ArithmeticException: / by zero
1 / 0.0 --> Infinity
-1.0 / 0 --> -Infinity
0 / 0.0 --> NaN
int operate(int x, int y) { ... }
double operate(int x, double y) { ... }
double operate(double x, double y) { ... }
max = left > right ? left : right;
|
own class (inc. nested class)
|
|
single type imports -> import p2.A;
|
|
class in same package
|
|
import on demand -> import p2.*
|
public enum Color {
BLUE(1), RED(2);
private final int code;
private Color(int code) {
this.code = code;}
public int getColorValue() {
return code;}}
people.sort(this::compareByAge)int compareByAge(Person p1, Person p2){return p1.age - p2.age;} |
people.sort(Sorter::compareByAge)static int compareByAge(Person p1, Person p2){return p1.age - p2.age;} |
Sorter sorter = new Sorter(); |
OuputStream fos = new FileOutputStream("serial.bin");
try (ObjectOutputStream stream = new ObjectOutputStream(fos)){
stream.writeObject(person);
}
try (BufferedInputStream inputBuffer = new *bis*(new FileInputStream(pathSource));
BufferedOutputStream outputB = new *bos*(new FileOutputStream(pathTarget))) {
byte[] byteBuffer = new byte[4096];
int value = inputBuffer.read(byteBuffer);
while (value >= 0) {
outputB.write(byteBuffer, 0, value);
value = inputBuffer.read(byteBuffer);}}
catch (FileNotFoundException e) {
System.out.println("File not found: "+e);
} catch (IOException e) {
System.out.println("reading Error:"+e);}
String a = "OO", b = "Prog", c = "OOProg";
//true
a == "OO"; c == "OO" + "Prog"; (a+b).equals(c);
//false
a + b == c;
|
Array
|
int[] intArray = new int[3]; |
|
List
|
List<T> al = new ArrayList<>(); |
|
Set
|
Set<T> hs = new HashSet<>(); |
|
Map
|
Map<U,T> hm = new HashMap<>(); |
Iterator<T> it = a.iterator(); while(it.hasNext()) {..} |
|
Collections.sort(Object o) |
needs Comparable on o
|
for (Map.Entry<Character, Double> entry: map.entrySet()) |
|
|
Methoden
|
implizit public, abstract
|
evt abstract, nie private
|
|
Variablen
|
KONSTANTEN (impl. public static final)
|
normal, Constructor mit super()
|
|
Vererbung
|
implements a,b |
extends a |
public abstract void report(); kein Rumpf{}void report();default void report(){...}interface I1 extends I2,I3class C2 extends C1 implements I1, I2I1.super.methode(); I2.super.methode();
|
Unchecked
|
Checked
|
|
Error
|
Try/catch oder Methodenkopf
|
|
RunTime-
Exceptions |
mögl. catch-Block:
e.printStackTrace() |
|
-> RunTime, NullPointer, IllegalArgument, IndexOutOfBounds
|
|
class myE extends Exception { myE() {}MyE(String message) {super(message);} }
@Before
public void setUp() {...}
@Test (timeout = 500, expected = Exception.class)
public void testGoodName(){
assertEquals(expected, actual);
assertTrue(condition);}
@Override
public boolean equals(Object obj) {
if(this == obj) {return true;}
if (obj == null) {return false;}
if (getClass() != obj.getClass()) {
return false;}
Student other = (Student)obj;
return regNumber == other.regNumber;}}
@Override
public int hashCode() {
return 31* firstName.hashCode()
+ 31 * lastName.hashCode();
}
@FunctionalInterface
interface Comparator<T> {
int compare(T first, T second);
}
|
Pattern pattern = Pattern.compile("reg")
|
|
|
vor ? optionaler Teil
|
([0]?[0-9]|2[0-3])
|
|
(){}*+?|\ als norm text
|
* \( \\ ...
|
|
Gruppennamen
|
(?<NAME>)
|
|
Matcher matcher = pattern.matcher(string);
if (matcher.matches()){String one = matcher.group("NAME")} |
|
Da lernt wohl jemand gerade genau dasselbe wie ich ;) Wäre es möglich, noch Covarianz und Contravarianz bei den Generics aufzunehmen? Helfe sonst gerne mit! => "lroellin@..."
Sry hab den Kommentar erst jetzt gesehen. Varianz war kein Prüfungsstoff. Hoffe es ist dir gut gelaufen =)
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.