Java: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
(7 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 10: | Zeile 10: | ||
== Reguläre Ausdrücke == | == Reguläre Ausdrücke == | ||
< | <syntaxhighlight lang="java"> | ||
Pattern rexpr = Pattern.compile("Name: (\\w+)"); | |||
Matcher matcher; | Matcher matcher; | ||
while( (line = reader.readLine()) != null){ | while( (line = reader.readLine()) != null){ | ||
Zeile 17: | Zeile 18: | ||
} | } | ||
} | } | ||
</ | |||
String text = "Egor Alla Anna"; | |||
Pattern pattern = Pattern.compile("A.+?a"); | |||
Matcher matcher = pattern.matcher(text); | |||
while (matcher.find()) { | |||
int start=matcher.start(); | |||
int end=matcher.end(); | |||
System.out.println("Match found" + text.substring(start,end) + " с "+ start + " By " + (end-1) + "position"); | |||
} | |||
System.out.println(matcher.replaceFirst("Ira")); | |||
System.out.println(matcher.replaceAll("Olga")); | |||
System.out.println(text); | |||
</syntaxhighlight> | |||
== Casting == | |||
<syntaxhighlight lang="java"> | |||
Staff staff = null; | |||
if(universityUser instanceof Staff) | |||
staff = (Staff)universityUser; | |||
</syntaxhighlight> | |||
== Container == | |||
=== Map === | |||
<syntaxhighlight lang="java"> | |||
Map<String, int> priorities = new HashMap<>(); | |||
priorities.put("Bob", 2); | |||
for (String key :priorities.keySet()){ | |||
int value = priorities.get(key); | |||
} | |||
if (! priorities.containsKey("Bob")){ | |||
error("missing Bob"); | |||
} | |||
</syntaxhighlight> | |||
== Besonderheiten == | |||
* Keine Default-Werte bei Parametern. Mit Overloading kann das simuliert werden. | |||
== Klassen == | |||
<syntaxhighlight lang="java"> | |||
public abstract class Widget { | |||
public enum WidgetType { WT_UNDEF, WT_POINT, WT_POLYLINE, WT_TEXT, WT_IMAGE }; | |||
protected int x; | |||
protected int y; | |||
protected String color; | |||
protected WidgetType type; | |||
Widget(WidgetType type, int x, int y, String color){ | |||
this.x = x; | |||
this.y = y; | |||
this.type = type; | |||
this.color = color; | |||
} | |||
public abstract void draw(Sheet sheet); | |||
} | |||
public class PointWidget extends Widget{ | |||
protected int size; | |||
PointWidget(int x, int y, String color, int size){ | |||
super(WidgetType.WT_POINT, x, y, color); | |||
this.size = size; | |||
} | |||
@Override | |||
public void draw(Sheet sheet) { | |||
// ... | |||
} | |||
} | |||
</syntaxhighlight> |
Aktuelle Version vom 9. November 2024, 14:30 Uhr
Resource aus Jar lesen[Bearbeiten]
InputReader input = getClass().getResourceAsStream("appl.properties"); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line; while( (line = reader.readLine()) != null) doIt(line);
Reguläre Ausdrücke[Bearbeiten]
Pattern rexpr = Pattern.compile("Name: (\\w+)");
Matcher matcher;
while( (line = reader.readLine()) != null){
if ( (matcher = rexpr.matcher(line)).find()){
name = matcher.group(1);
}
}
String text = "Egor Alla Anna";
Pattern pattern = Pattern.compile("A.+?a");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
int start=matcher.start();
int end=matcher.end();
System.out.println("Match found" + text.substring(start,end) + " с "+ start + " By " + (end-1) + "position");
}
System.out.println(matcher.replaceFirst("Ira"));
System.out.println(matcher.replaceAll("Olga"));
System.out.println(text);
Casting[Bearbeiten]
Staff staff = null;
if(universityUser instanceof Staff)
staff = (Staff)universityUser;
Container[Bearbeiten]
Map[Bearbeiten]
Map<String, int> priorities = new HashMap<>();
priorities.put("Bob", 2);
for (String key :priorities.keySet()){
int value = priorities.get(key);
}
if (! priorities.containsKey("Bob")){
error("missing Bob");
}
Besonderheiten[Bearbeiten]
- Keine Default-Werte bei Parametern. Mit Overloading kann das simuliert werden.
Klassen[Bearbeiten]
public abstract class Widget {
public enum WidgetType { WT_UNDEF, WT_POINT, WT_POLYLINE, WT_TEXT, WT_IMAGE };
protected int x;
protected int y;
protected String color;
protected WidgetType type;
Widget(WidgetType type, int x, int y, String color){
this.x = x;
this.y = y;
this.type = type;
this.color = color;
}
public abstract void draw(Sheet sheet);
}
public class PointWidget extends Widget{
protected int size;
PointWidget(int x, int y, String color, int size){
super(WidgetType.WT_POINT, x, y, color);
this.size = size;
}
@Override
public void draw(Sheet sheet) {
// ...
}
}