On my computer the path is
D:\Program Files (x86)\flex_sdk_4.1\bin
Usually on windows you append this to the path variable.
D:\Program Files (x86)\flex_sdk_4.1\bin
public class Test { public static void main(String[] args) { printAll(intersection(new int[]{-5} ,new int[]{6,55,66,-4,-5} ) ); } static int [] intersection(int array1[] , int array2[]){ java.util.ArrayList<Integer> arrayList = new java.util.ArrayList<Integer>(); for(int outer=0; outer< array1.length ; outer++){ for(int inner=0; inner < array2.length ; inner++){ if(array1[outer] == array2[inner]){ arrayList.add(array1[outer]); // or arrayList.add(array2[outer]); will do } } } int intersectionArray[] = new int[arrayList.size()]; // now add all the elements in arrayList to intersectionArray[] for(int i = 0 ; i < arrayList.size() ; i++){ intersectionArray[i] = arrayList.get(i); } return intersectionArray; } static void printAll(int array[]){ for(int i =0 ; i < array.length ; i++){ System.out.println(array[i]); } } }
static int [] indexOfArray(char character, String word){ java.util.ArrayList<Integer> arrayList = new java.util.ArrayList<Integergt;(); // loop through word to find the occurrence of character for(int i = 0 ; i < word.length() ; i++){ if( word.charAt(i) == character){ arrayList.add(i); // if character found add i to arrayList } } int [] array = new int[arrayList.size()]; int i =0; // add all elements in arrayList to int array for(int x : arrayList){ array[i] = arrayList.get(i); i++; } return array; } static void printAll(int array[]){ for(int i =0; i < array.length ; i++){ System.out.println(array[i]); } }
static boolean isNumber(String number){ if(number.charAt(0)=='-'){ for(int i = 1 ; i < number.length() ; i++){ if( Character.isDigit(number.charAt(i))){ } else{ return false; } } } else{ for(int i = 0 ; i < number.length() ; i++){ if( Character.isDigit(number.charAt(i))){ } else{ return false; } } } return true; }Well the above code can be simplified alot with the use of ternary operator
static boolean isNumber(String number){ for(int i = number.charAt(0)=='-' ? 1: 0 ; i < number.length() ; i++){ if( Character.isDigit(number.charAt(i))){ } else{ return false; } } return true; }
public static void main(String[] args) { System.out.println(vaidateEmailAddress("exa.mple@gmail.com") ); System.out.println(vaidateEmailAddress("exa..mple@gmail.com") ); } static boolean vaidateEmailAddress(String email){ boolean valid = false; String domain[] = {"gmail.com" , "yahoo.com", "hotmail.com" }; String localPart = email.substring(0, email.indexOf('@', 0)); String domainPart = email.substring( localPart.length() + 1, email.length()); int noOfAts = 0; for(int i = 0 ; i < email.length(); i++){ if( email.indexOf('@', i) != -1){ noOfAts++; i = email.indexOf('@', i); } } if(noOfAts > 1 || noOfAts == 0) { // if more than one @ symbol appears no point in further checking. Its a invalid email address return false; } else{ // now that it has been found that there is only one @ symbol lets check the domain for( int i = 0 ; i < domain.length ; i++){ if( domainPart.equals(domain[i])){ valid = true; // still further processing of the string is needed. Thats why it doesn't return true } } } // checks for the presence of invalid characters in the local part if(localPart.charAt(0) == '.' || localPart.charAt(localPart.length() -1) == '.'){ return false; } else{ for(int i =0 ; i < email.indexOf('@', 0); i++){ if(validCharacter(email.charAt(i)) ){ } else if(email.charAt(i)== '.' && email.charAt(i+1)== '.'){ return false; } else if(email.charAt(i)=='.'){ } else{ return false; } } } return valid; } static boolean validCharacter(char c){ char validSymbols[] = {'!', '#' ,'$' ,'%' ,'&' ,'\'' ,'*' ,'+', '-', '/', '=' ,'?', '^','_','`' ,'{' ,'|' ,'}', '~'}; for(int i =0 ; i < validSymbols.length; i++){ if(validSymbols[i] == c ){ } else{ if(Character.isLetter(c)){ } else if(Character.isDigit(c)){ } else{ return false; } } } return true; } }
int array[] = {4,5,22,33}; int a = array[6];
int array2[] = null; int b = array2[1];
String string = "Hello World"; String subString = string.substring(-1, 5);
int a = 4; int b = a/0;
Stack<integer> ese = new Stack<integer>(); ese.pop();
int no=0; InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inputStreamReader); no = Integer.parseInt(in.readLine() );
static long sof(long n){ if(n==4) return 1; else return n + (sof(n-1)) ; }
static void readTo2DArray() throws IOException {
FileInputStream fstream = new FileInputStream("test.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int arraySize = 6;
String array[][] = new String[arraySize][];
int index = 0;
while ((strLine = br.readLine()) != null) {
if (index >= arraySize - 1) {
System.out.println("Error : Increase array size !");
break;
}
array[index] = strLine.split(" ");
index++;
}
printAll(array);
}
static void printAll(String array[][]) {
for (int i = 0; i < array.length; i++) {
if (array[i] != null) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println(" ");
}
}
}
static void printAll(int number) { while (number > 10) { int digit = number % 10; number = (number - digit) / 10; System.out.print(digit + " "); } System.out.print(number); }
static void printAll(int number) { String numberString = Integer.toString(number); for(int i = numberString.length()-1 ; i >=0 ; i--){ System.out.print(numberString.charAt(i) + " "); } }
static int rev(int number) { int rev = 0; while (true) { int digit = number % 10; number = (number - digit) / 10; rev += digit; if (number < 10) { rev *= 10; rev += number; break; } else { rev *= 10; } } return rev; }Method 2 - Using StringBuffer and String
static int rev2(int number){ int rev2 = 0; String numString = Integer.toString(number); String rev2String = ""; StringBuffer stringBuffer = new StringBuffer(); for(int i = numString.length() - 1 ; i >=0; i--){ stringBuffer.append(numString.charAt(i)); } rev2String = stringBuffer.toString(); rev2 = Integer.parseInt(rev2String); return rev2; }
static void method1(int number){ if(number % 10 == 1 || number % 10 == 3 || number % 10 == 5 || number % 10 == 7 || number % 10 == 9){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
static void method2(int number){ if(number % 2 == 1){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
static void method3(int number){ if(Integer.lowestOneBit(number) == 1){ System.out.println("ODD"); } else{ System.out.println("EVEN"); } }
static void foo() { Vector<String[]> vector = new Vector<String[]>(); String array1[] = { "word1", "word2", "word3" }; String array2[] = { "word4", "word5", "word6" }; String array3[] = { "word7", "word8", "word9" }; vector.add(array1); vector.add(array2); printall(vector); vector.set(1, array3); printall(vector); } static void printall(Vector<String[]> vector) { System.out.println("\n"); for (int i = 0; i < vector.size(); i++) { for (int j = 0; j < vector.get(i).length; j++) { System.out.println(vector.get(i)[j]); } } } }
static void foo(){ Vector<String []> vector = new Vector<String []>(); String array1[] = {"word1","word2","word3"}; String array2[] = {"word4","word5","word6"}; String array3[] = {"word7","word8","word9"}; vector.add(array1); vector.add(array2); vector.add(array3); printall(vector); String newarray[] = vector.get(1); newarray[0]="newword"; printall(vector); } static void printall(Vector<String []> vector){ System.out.println("\n"); for(int i =0; i < vector.size();i++){ for(int j=0; j < vector.get(i).length ; j++){ System.out.println(vector.get(i)[j]); } }
String s1 = "hello\r world \nhello world"; System.out.println(s1); String s2 = s1.replaceAll("[\n\r]", ""); System.out.println(s2);
String s1 = "hello\r world \nhello world"; String s2 =""; for(int j =0 ; j < 2 ; j++){ for(int i = 0 ; i < s1.length() ; i++){ if(s1.indexOf( j==0 ? '\n' : '\r' , i) != -1){ s2 += s1.substring(i, s1.indexOf(j==0 ? '\n' : '\r', i)); i = s1.indexOf(j==0 ? '\n' : '\r', i); } else{ s2+=s1.substring(i, s1.length()); break; } } if(j==0){ s1 = s2; s2=""; } } System.out.println(s2);Method 2:- Using StringBuffer. This is a much cleaner method
StringBuffer stringBuffer = new StringBuffer(); String s1 = "hello\r world \nhello world"; for(int i = 0 ; i < s1.length() ; i++){ if(s1.charAt(i) != '\n' && s1.charAt(i) != '\r' ){ stringBuffer.append(s1.charAt(i)); } } System.out.println(stringBuffer.toString());
static long factorial(int factorial) { int fact = 1; for (int i = factorial; i > 0; i--) { fact *= i; } return fact; }
public static long factorial( int n ) { if( n <= 1 ) // base case return 1; else return n * factorial( n - 1 ); }
void method1(){ int array[] = {3,4,33,22}; method2(array); } void method2(int array[]){ // modify array contents }
class intarray{ int array[] = {3,4,33,22}; } void method1(){ intarray object = new intarray(); method2(objectarray); } void method2(int array[]){ // modify array contents }
import java.util.Random; import java.util.Random; class integer { // this class is created to pass the array as a reference int array[] = { 55, 44 }; } public class shuffle { public static void main(String[] args) { integer object = new integer(); shuffleByRandomSwapping(object.array, 2); printAll(object.array); } static void printAll(int array[]) { for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } static void shuffleByRandomSwapping(int array[], int amount) { Random generator = new Random(); if (array.length == 1) { // no meaning of swapping return; } else for (int i = 0; i < amount; i++) { if (array.length == 2) { if (amount % 2 != 0) { // just a simple swap int temp = array[0]; array[0] = array[1]; array[1] = temp; } else { // no effective swapping takes place return; } } else { int index1 = generator.nextInt(array.length); int index2 = generator.nextInt(array.length); while (index1 == index2) { // if index1==index2 no swapping takes place index2 = generator.nextInt(array.length); } int temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; } } } }
import java.util.Random; public class Matrix implements Runnable { static int matrixSize = 5000; static long mult = 0; static int m1[]; static int m2[]; static void init() { m1 = new int[matrixSize]; m2 = new int[matrixSize]; Random randomGenerator = new Random(); for (int i = 0; i < matrixSize; i++) { m1[i] = randomGenerator.nextInt(10); m2[i] = randomGenerator.nextInt(10); } } public static void main(String[] args) { init(); // create two threads Thread th1 = new Thread(new Matrix()); Thread th2 = new Thread(new Matrix()); // sets names th1.setName("1"); th2.setName("2"); // start threads th1.start(); th2.start(); try { // joins threads to main thread th1.join(); th2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("mult" + mult); } public void run() { if (Thread.currentThread().getName().equals("1")) { long localMult = 0; for (int i = 0; i < matrixSize / 2; i++) { localMult += m1[i] * m2[i]; } sum(localMult); } else if (Thread.currentThread().getName().equals("2")) { long localMult = 0; for (int i = matrixSize / 2; i < matrixSize; i++) { localMult += m1[i] * m2[i]; } sum(localMult); } } synchronized void sum(long amount) { // this method is synchronized because this updates global variable System.out.println("Sum " + amount); mult += amount; } }
import java.util.ArrayList; public class ForEach { static void array(){ double[] array = {9.5, 4.3, 3.8, 8.8}; double sum = 0; for (double d : array) { sum += d; } System.out.println(sum); } static void arrayList(){ ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(1); arrayList.add(2); arrayList.add(3); arrayList.add(4); arrayList.add(5); for (int integer : arrayList) System.out.print(integer + " "); } public static void main(String args[]){ array(); arrayList(); } }
message = How are you
import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class testmain { public static void main(String[] args) { Properties props = new Properties(); try { props.load(new FileInputStream("message.properties")); String message = props.getProperty("message"); System.out.println(message); } //catch exception in case properties file does not exist catch(IOException e) { e.printStackTrace(); } } }
export JAVA_HOME=/somedirectory/jdk1.5.0_08/
libaio.so.1 shared object file not found
sudo aptitude install libaio-dev