payal-kothari
6/22/2017 - 7:03 AM

java syntax notes

java syntax notes copy


* There is no "get(int index)" method for
hashSet in java

1) Convert string to char array:

char[] ch = nameStr.toCharArray(); 
ch t = ch[0];


2) Convert char to string:

Use StringBuilder.

char ch = 'A';

String str = "" + ch;

3) set char value to empty or null.

This will set value to '\u0000'.

If you don't initialize char[] , all values in it will be equal to '\u0000'.

You can check if (char[4] == Character.MIN_VALUE)

chArr[2] = Character.MIN_VALUE;


4)  If you want to just reverse the original
arraylist without saving original copy:

Collections.reverse(origArrayList);



>>>> If you want to store both original and reversed arraylist :

ArrayList reversed = new ArrayList() ; 
for ( int i = orig.size() - 1 ; i >= 0 ; i-- ) 
{ 
    Object obj = orig.get( i ) ; 
    reversed.add( obj ) ; 
} 


5) 

// create an array of strings 
   String a[] = new String[]{"abc","klm","xyz","pqr"}; 

   List list1 = Arrays.asList(a);

Or

List list1 = Arrays.asList(3, 2, 4);

6) sort an array:

http://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/
(http://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/) 
int [] arr = { 13 ,  7 ,  6 ,  45 ,  21 ,  9 ,  101 ,  102 };
 
         Arrays.sort(arr);



// Note that we have Integer here instead of
         // int[] as Collections.reverseOrder doesn't
         // work for primitive types.
         Integer[] arr = { 13 ,  7 ,  6 ,  45 ,  21 ,  9 ,  2 ,  100 };
 
         // Sorts arr[] in descending order
         Arrays.sort(arr, Collections.reverseOrder());




7)convert char[] to string:

String s = "str";

char[] cArr = s.toCharArray();

String ss = String.valueOf(cArr);

8)


str.charAt(int index);



9)

   int[] nums = {-2147483648,-2147483648,-2147483648,-2147483648,1,1,1};     


Integer max3 = null;   

        for(Integer n : nums){

            if(n.equals(max3)  ){       // IMP********

           }