Skip to content

Instantly share code, notes, and snippets.

@shicky
Last active July 13, 2016 06:11
Show Gist options
  • Save shicky/48983e0c4f58fbeea8e33745a6472580 to your computer and use it in GitHub Desktop.
Save shicky/48983e0c4f58fbeea8e33745a6472580 to your computer and use it in GitHub Desktop.
Java Syntax
public static void main(String[] args) {
// data type and max values
/*
width minimum maximum
byte: 8 bit -128 +127
short: 16 bit -32 768 +32 767
int: 32 bit -2 147 483 648 +2 147 483 647
long: 64 bit -9 223 372 036 854 775 808 +9 223 372 036 854 775 807
float: 32 bit 1.4E-45 3.402,823,5E+38
double: 64 bit 1.797,693,134,862,315,7E308 4.9E-324
BigInteger (2 ^ 32) ^ Integer.MAX_VALUE
*/
// Scanner
Scanner in = new Scanner(System.in);
// Get string
String time = in.next();
// Get ints
int a;
a = in.nextInt();
int b;
b = in.nextInt();
int sum;
sum = solveMeFirst(a, b);
System.out.println(sum);
// Array
int n = in.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
// Double Array
int a[][] = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
}
}
// BigInteger
BigInteger sum = BigInteger.ZERO;
sum = sum.add(BigInteger.valueOf(5));
// Math
Math.abs(-5);
// Round up
(int) Math.ceil((double)a / 100);
// String manipulation
StringBuilder sb = new StringBuilder();
sb.append("hello");
sb.append(" world!");
// String
String hello = "Hello World";
// length
int length = hello.length();
// concat
hello = hello.concat(" and Steve!");
hello = hello + "SDGFSDF";
// format
String.format("%d is blah %s", 5, "Steve");
char c = hello.charAt(5);
boolean b = hello.equalsWith(hello);
string full = "0123456789";
String partial = full.substring(3,7); // 3456
// convert string to int
Integer.parseInt("5454");
// iterate string
String iterate = "Iterating...";
for (int i = 0; i < iterate.length(); i++) {
chat c= s.chartAt(i);
int ascii = (int) c;
}
for(char c : iterate.toCharArray()) {
// do something with c
}
}
// Format Padding
System.out.format("%02d", 3); // 03
// % 0 2 d
// format pad with zero width int
// Array Init
int arr[] = new int[5]; // array of size 5
int arr2[] = new int[]{1,2,3};
int arr3[] = {1,2,3};
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
// init with default value (default value is actually 0 if not specified)
int temp[] = new int[100];
Arrays.fill(temp, -1);
// LinkedList
LinkedList myList = new LinkedList<Integer>();
myList.add(1);
myList.add(2);
System.out.println(myList);
System.out.println(myList.size());
// LinkedList implements interfaces Collection, Deque, List, Queue
// Queue
Queue<Integer> st1=new LinkedList<Integer>();
/*
Throws exception Return value value
insert add(e) offer(e) False (full)
remove remove() poll() null (empty)
examine element() peek() null (empty)
isEmpty() - check if empty
*/
// PriorityQueue - MinHeap - top value is minimum
PriorityQueue<Integer> queue = new LinkedList<Integer>();
// Reversed PriorityQueue - MaxHeap - top value is maximum
PriorityQueue<Integer> maxPQ = new PriorityQueue<Integer>(20,Collections.reverseOrder());
// HashMap
// !!!!! NOT ALLOWED TO USE PRIMITIVE!!!
HashMap<Character, Integer> count = new HashMap<>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment