Tahák na OSTRAJavu
Před začátkem testu si tento soubor stáhněte, abyste splnili požadavek, že jde o off-line zdroj.
Povolené on-line zdroje:
Dědičnost
public class PrefixPrinter extends GenericPrinter { }
Packages
package cz.nsalitomerice.java.logger;
import cz.nsalitomerice.java.logger.SinglePrinterLogger;
Iterátory
Iterator<Object> iter = foo.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
Interface
public interface MyCollection {
public void add(Object o);
public Object get(int i);
int size();
}
...
public class MyImpl implements MyCollection, Iterable<Object> { ... }
Pole
char[] newarr = new char[666];
Arraycopy
System.arraycopy(zdroj, zdrojpos, cíl, cílpos, kolik);
2D pole, pole s inicializací
int[][] multi = new int[5][10];
int[][] multi = new int[][]{
{ 1, 2, 3},
{ 9, 8, 7} };
Výjimky
try {
return arr[i];
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("idx out of range");
return 0;
}
Vyrobení iterátoru
public Iterator<Character> iterator() {
return new Iterator() {
private int index = 0;
@Override
public boolean hasNext() {
return index < ptr;
}
@Override
public Character next() {
return arr[index++];
}
};
}
Načtení souboru
DataInputStream di = new DataInputStream(
new BufferedInputStream (
new FileInputStream("passwd")));
ArrayList<String> users = new ArrayList<>();
ArrayList<Integer> uids = new ArrayList<>();
int sect = 0;
StringBuilder el = new StringBuilder();
while(true) {
int r = di.read();
if (r<0) {
break;
}
if(r == ':') {
if(sect == 0) {
users.add(el.toString());
}
if(sect == 2) {
uids.add(Integer.parseInt(el.toString()));
}
sect++;
el = new StringBuilder();
} else if(r == '\n') {
sect = 0;
el = new StringBuilder();
} else {
el.append((char)r);
}
}
Načtení vstupu od uživatele
Scanner scanner = new Scanner(System.in);
String user = scanner.next();
int uid = scanner.nextInt();
Zapsání souboru
DataOutputStream ds = new DataOutputStream(
new BufferedOutputStream (
new FileOutputStream("passwd", true)));
ds.writeChars("ahoj");
ds.close();
Je adresář? Zkopírování souboru.
java.nio.file.Path p2 = java.nio.file.Paths.get(args[1]);
if(Files.isDirectory(p2)) {
java.nio.file.Path srcpath = java.nio.file.Paths.get(infile);
java.nio.file.Path dstpath = java.nio.file.Paths.get(args[1] + "/" + infile);
java.nio.file.Files.copy(srcpath, dstpath, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
System.exit(0);
}
File streams
DataInputStream di = new DataInputStream(
new BufferedInputStream (
new FileInputStream(infile)));
DataOutputStream ds = new DataOutputStream(
new BufferedOutputStream (
new FileOutputStream("soutfile.txt")));
while(true) {
int r = di.read();
if (r<0) {
break;
}
ds.write(r);
}
ds.flush();
di.close();
ds.close();
Měření času
long before = System.nanoTime();
Threading
public class mergeThr extends Thread {
public void run() { }
Thread t = new mergeThr(0, a.length, a, depth);
t.start();
t.join();
MIN_VALUE
int x = Integer.MIN_VALUE;
Sort, lambda
Collections.sort(lines, (String p1, String p2) -> {
if("-l".equals(args[0])) {
return p1.compareTo(p2);
} else {
return p2.compareTo(p1);
}
});
TCP server
public class ClientThread extends Thread {
ClientThread(Socket socket) throws IOException {
this.socket = socket;
}
public void run() {
synchronized(ClientThread.class) {
Reader re = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(re);
this.out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
while (true) {
String arg = br.readLine();
out.println("ahoj");
}
}
}
ServerSocket s = new ServerSocket(6666);
s.setReuseAddress(true);
while(true) {
try {
Socket socket = s.accept();
Thread t = new ClientThread(socket);
t.start();
} catch (Exception e) {
System.out.println("cannot accept");
break;
}
}
TCP klient
InetAddress addr = InetAddress.getByName("localhost");
try (Socket socket = new Socket(addr, 6666)) {
Reader re = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(re);
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
}
String l = br.readLine();
out.println("-d");
isWhitespace
Character.isWhitespace((char) c)
Collections
String[] a; a.length;
ArrayList<String> exp = new ArrayList<>();
exp.add("foo");
int j = exp.size();
exp.remove(exp.size()-1); // remove last
inkedList<Double> numstack = new LinkedList<>();
peek() = pop(), push(), size()
HashMap<String, Double> mapa = new HashMap<>();
mapa.put("last", 1.2);
mapa.containsKey(k);
mapa.get(k);