Professional Documents
Culture Documents
Integer To String
Integer To String
Integer To String
int i = 42;
String str = Integer.toString(i);
or
String str = "" + i
double to String :
String str = Double.toString(i);
long to String :
String str = Long.toString(l);
float to String :
String str = Float.toString(f);
String to integer :
str = "25";
int i = Integer.valueOf(str).intValue();
or
int i = Integer.parseInt(str);
String to double :
Double d = Double.valueOf(str).doubleValue();
String to long :
long l = Long.valueOf(str).longValue();
or
Long l = Long.parseLong(str);
String to float :
Float f = Float.valueOf(str).floatValue();
decimal to binary :
int i = 42;
String bin = Integer.toBinaryString(i);
decimal to hexadecimal :
int i = 42;
String hexstr = Integer.toString(i, 16);
or
String hexstr = Integer.toHexString(i);
char to String
String s = String.valueOf('c');
integer to ASCII code (byte)
char c = 'A';
int i = (int) c; // i == 65 DECIMAL
To extract Ascii codes from a String
String test = "ABCD";
for ( int i = 0; i < test.length(); ++i ) {
char c = test.charAt( i );
int j = (int) c;
System.out.println(j);
}
integer to boolean
b = (i != 0);
// ex : 42 != 0 --> true
boolean to integer
i = (b)?1:0;
// true --> 1
Note :To catch illegal number conversion, use the try/catch mechanism :
try{
i = Integer.parseInt(aString);
}
catch(NumberFormatException e) {
...
}
Check numeric digit (check the minus sign and decimal seperator)
/**
* Checks whether the supplied character is a letter or number.
*/
public static boolean isLetterOrNumber(int c) {
return isLetter(c) || isNumber(c);
}
/**
* Checks whether the supplied character is a letter.
*/
public static boolean isLetter(int c) {
return isUpperCaseLetter(c) || isLowerCaseLetter(c);
}
/**
* Checks whether the supplied character is an upper-case letter.
*/
public static boolean isUpperCaseLetter(int c) {
return (c >= 65 && c <= 90); // A - Z
}
/**
* Checks whether the supplied character is an lower-case letter.
*/
public static boolean isLowerCaseLetter(int c) {
return (c >= 97 && c <= 122); // a - z
}
/**
* Checks whether the supplied character is a number
*/
public static boolean isNumber(int c) {
return (c >= 48 && c <= 57); // 0 - 9
}
}
trim string
Str.trim();
Method Description
Evaluates if "regex" matches s. Returns only true if the WHOLE string
s.matches("regex")
can be matched.
Creates an array with substrings of s divided at occurrence of "regex".
s.split("regex")
"regex" is not included in the result.
s.replaceFirst("regex"),
Replaces first occurance of "regex" with "replacement.
"replacement"
s.replaceAll("regex"),
Replaces all occurances of "regex" with "replacement.
"replacement"
package de.vogella.regex.string;
StringTokenizer
StringTokenizer st = new StringTokenizer(str,” “);//” ” separate by anything
while(st.hasMoreTokens()) {
String s = st.nextToken();
}
ArrayList
//To go through each item in your ArrayList you can set up something called an
Iterator. This class can also be found in the java.util library:
import java.util.Iterator;
Iterator it = listTest.iterator( );
This sets up a new Iterator object called it that can be used to go through the items
in the ArrayList called listTest. The reason for using an Iterator object is because
it has methods called next and hasNext. You can use these in a loop:
while ( it.hasNext( ) ) {
System.out.println( it.next( ) );
}
return sb.reverse().toString();
}
Regular Expression (RegEx)
Pattern pattern = Pattern.compile(<REGEX>);
Matcher matcher = patter.matcher(<INPUT ANY STRING>);
Dijkstra
import java.util.InputMismatchException;
import java.util.Scanner;
{
private boolean settled[];
private boolean unsettled[];
private int distances[];
private int adjacencymatrix[][];
private int numberofvertices;
Fibonacci
fib(num2,num2+num1);
}
Biginteger
import java.math.BigInteger;
// Create via a string
BigInteger bi1 = new BigInteger("1234567890123456890");
bi1 = bi1.add(bi2);
bi1 = bi1.multiply(bi2);
bi1 = bi1.subtract(bi2);
bi1 = bi1.divide(bi2);
bi1 = bi1.mod(bi2);
int exponent = 2;
bi1 = bi1.pow(exponent);
BigInteger bi = new BigInteger("1023");
// Parse and format to binary
bi = new BigInteger("1111111111", 2);
String s = bi.toString(2);
Factorial
Permutation
Combination
Prime number
boolean isPrime(int n) {
//check if n is a multiple of 2
if (n%2==0) return false;
//if not, then just check the odds
for(int i=3;i*i<=n;i+=2) {
if(n%i==0)
return false;
}
return true;
}
LinkedList
public class LinkedList {
private Node first; //the 1st node
private Node last; //the last node
private Node current; //if any
public LinkedList(){
first = null; //set the linked list to null
last = null;
current = null;
}
if (isEmpty())
{
first = newNode;
last = newNode;
}
else
{
newNode.next = first;
first = newNode;
}
}
public void insertAtBack(Object insertItem)
{
Node newNode = new Node(insertItem);
if(isEmpty())
{
first = newNode;
last = newNode;
}
else
{
last.next = newNode;
last = newNode;
}
}
if(isEmpty())
{
return removeItem;
}
removeItem = first.data;
if(first == last)
{
first = null;
last = null;
}
else
first = first.next;
return removeItem;
}
if(isEmpty())
{
return removeItem;
}
removeItem = last.data;
if (first == last)
{
first = null;
last = null;
}
else
{
current = first;
while(current.next != last)
current = current.next;
last = current;
last.next = null;
}
return removeItem;
}
Node
Stack
public class Stack
{
private Node first;
private Node last;
private Node current;
public Stack(){
first = null;
last = null;
current = null;
}
if(isEmpty()) {
return removeItem;
}
removeItem = first.data;
if(first == last) {
first = null;
last = null;
}
else {
first = first.next;
}
return removeItem;
}
G.bfs();
}
}
import java.util.*;
boolean[] visited;
/* -------------------------------
Construct a graph of N nodes
------------------------------- */
Graph1(int N)
{
NNodes = N;
adjMatrix = new int[N][N];
visited = new boolean[N];
}
Graph1(int[][] mat)
{
int i, j;
NNodes = mat.length;
q.add(rootNode);
visited[rootNode] = true;
printNode(rootNode);
while( !q.isEmpty() )
{
int n, child;
n = (q.peek()).intValue();
child = getUnvisitedChildNode(n);
if ( child != -1 )
{
visited[child] = true;
printNode(child);
q.add(child);
}
else
{
q.remove();
}
}
int getUnvisitedChildNode(int n)
{
int j;
for ( j = 0; j < NNodes; j++ )
{
if ( adjMatrix[n][j] > 0 )
{
if ( ! visited[j] )
return(j);
}
}
return(-1);
}
void clearVisited()
{
int i;
void printNode(int n)
{
System.out.println(n);
}
}