Sunday, February 15, 2015

Array Functions

We have couple of arrays functions that could be handy to perform some array operations.Listed below
- Copying array
- Copying array of a particular range
- Sorting Array
- search array


package Example1;

import java.util.Arrays;

public class CopyArray {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] array1=new int[10];
		int randomnumber;
		for(int i=0;i< array1.length;i++){
			randomnumber=(int)(Math.random()*51);
			array1[i]=randomnumber;
		}
		
		for(int row:array1){
			System.out.print(" "+row+" ");
		}
		//copy array
		System.out.println();
		int[] copyarray=Arrays.copyOf(array1, 3);
		for(int row:copyarray){
			System.out.print(" "+row+" ");
		}
		//copy array in a particular range
		System.out.println();
		int[] copyarray2=Arrays.copyOfRange(array1, 2, 6);
		for(int row:copyarray2){
			System.out.print(" "+row+" ");
		}
		
		//sort the array		
		System.out.println();
		Arrays.sort(array1);
                int exist=Arrays.binarySearch(array1, 50);
		System.out.println(exist);
		System.out.println(Arrays.toString(array1));
	}

}


output :
27 20 20 31 32 21 28 27 40 46
27 20 20
20 31 32 21
-ve value [20, 20, 21, 27, 27, 28, 31, 32, 40, 46]
>>>
------------------------------------------------------------------------------------------------------------

Enhanced Forloop


package Example1;

public class enhanceforloop {

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  int[] singlearray = new int[10];
  int k = 0;
  while (k <= 15) {
   System.out.print("-");
   k++;
  }
  System.out.println();

  for (int i = 0; i < singlearray.length; i++) {
   singlearray[i] = i;
  }
  for (int row : singlearray) {
   System.out.print(" " + row + " ");
  }
  System.out.println();

  k = 0;
  while (k <= 15) {
   System.out.print("-");
   k++;
  }
  System.out.println();

  char[][] multiarray = new char[10][10];

  for (int i = 0; i < multiarray.length; i++) {
   for (int j = 0; j < multiarray[i].length; j++) {
    multiarray[i][j] = '*';
   }
  }

  for (char[] row : multiarray) {
   for (char column : row) {
    System.out.print(" " + column + " ");
   }
   System.out.println();
  }
 }
}


>>>
------------------------------------------------------------------------------------------------------------

Arrays

Arrays can be of single dimension or multidimension, its a collection of similar datatypes. below example illustarte the creation of arrays.


package Example1;

import java.lang.reflect.Array;

public class CreateArray {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("single dimension array");
  int[] singlearray = new int[10];
  for (int i = 0; i < singlearray.length; i++) {
   singlearray[i] = i;
   System.out.print(" " + singlearray[i] + " ");
  }
  System.out.println();

  int k = 0;
  while (k <= 15) {
   System.out.print("-");
   k++;
  }
  System.out.println();
  
  
  System.out.println("multi dimension array");
  int[][] multiarray = new int[10][10];

  for (int i = 0; i < multiarray.length; i++) {
   for (int j = 0; j < multiarray[i].length; j++) {
    multiarray[i][j] = i;
    System.out.print("|" + multiarray[i][j] + "|");
   }
   System.out.println();
  }
 }

}


output :
single dimension array
0 1 2 3 4 5 6 7 8 9
----------------
multi dimension array
|0||0||0||0||0||0||0||0||0||0|
|1||1||1||1||1||1||1||1||1||1|
|2||2||2||2||2||2||2||2||2||2|
|3||3||3||3||3||3||3||3||3||3|
|4||4||4||4||4||4||4||4||4||4|
|5||5||5||5||5||5||5||5||5||5|
|6||6||6||6||6||6||6||6||6||6|
|7||7||7||7||7||7||7||7||7||7|
|8||8||8||8||8||8||8||8||8||8|
|9||9||9||9||9||9||9||9||9||9|
>>>
------------------------------------------------------------------------------------------------------------