API:
Compiling:
$ javac program.java
$ java program
Standard Java shell (taken from SCT 😉 ):
import java.io.*;
import java.util.*;
public class example
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("example.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("example.out")));
StringTokenizer st = new StringTokenizer(br.readLine());
//To read a single integer:
int x = Integer.parseInt(st.nextToken());
//To read multiple integers in one line:
int[] xs = new int[10];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 10; i++)
xs[i] = Integer.parseInt(st.nextToken());
br.close();
//To print:
pw.println(x + xs [0]);
pw.close();
}
}
Keywords:
class
public, private
static
void
null
abstract
etc…
Primitives:
boolean b = true;
byte by = 255;
char c = ‘A’;
short sh = 1000;
int i = 7;
long l = 123456789L;
float f = 3.14f;
double d = 3.14159;
Sort-of Primitives:
String s = “Hello World!”;
int[] array1d = new int[10];
int[][] array2d = new int[10][2];
int[][][] array3d = new int[10][100][2];
s.length();
s.charAt(i);
s.substring(i,j);
“Con” + “cat” + “enation”
array.length;
array[i];
Data Structures:
ArrayList<E> al = new ArrayList<E>();
LinkedList<E> ll = new LinkedList<E>();
Queue<E> q = new LinkedList<E>();
PriorityQueue<E> pq = new PriorityQueue<E>();
Stack<E> st = new Stack<E>();
HashSet<E> hs = new Set<E>();
TreeSet<E> ts = new Set<E>();
HashMap<E1, E2> hm = new Map<E1, E2>();
TreeMap<E1, E2> tm = new Map<E1, E2>();
Count, Add, Remove, Get, Set, Query, Iterate elements:
o.empty();
o.size();
o.add(E e);
o.remove(E e);
o.get(E e);
o.set(int i, E e2);
o.contains(E e);
Iterator it = o.iterator();
etc…
Making Objects:
class objectName {
public String var1;
private int var2;
public objectName(String s) {
var1 = s;
var2 = 0;
}
public void setVal(int newV) {
var2 = newV;
}
public int getVal() {
return var2;
}
public void increment() {
var2++;
}
}
class compObj implements Comparable<compObj> {
public int toComp;
public compObj(int i) {toComp = i;}
public boolean equals(compObj o) {
return this.toComp == o.toComp;
}
public int compareTo(compObj o) {
return this.toComp - o.toComp;
}
}
Advanced Stuff:
toString()
hashCode()
equals()
compareTo()
implements, extends
Programming Structures:
if(condition1) {…}
else if(condition2) {…}
else {…}
for(int i = 0; i < N; i++) {…}
for(int i : arrayList) {…}
while(condition) {…}
do {…} while(condition);
switch(var) {
case 1: …; break;
case 2: …; break;
case 3: …; break;
default: …; break;
}
(a <condition> b)? x:y;
continue;
break;
return x;
Conditionals and Operations:
+ – * / %
= += -= *= /= %=
x++ ++x x– –x
== != < <= > >= instanceof
&& || !
& ^ | ! ~ << >> >>>
&= ^= |= <<= >>= >>>=
Know when to use x = y, x == y, x.equals(y)
Useful Classes:
Arrays
Collections
String
Math
Integer/Double/etc.
BufferedReader
StringTokenizer
PrintWriter
System (System.out.println(“Hello World!”);)
Algorithms:
Binary Search
BFS / DFS / DFSID / Floodfill
Floyd-Warshall
Dijkstra
Bellman-Ford
Dynamic Programming Algorithms
Basic Geometry (Point, Polygon classes are useful 😉 )