Wednesday, 28 August 2013

Array

Array is the collection of  elements of same datatype stored in the contigious memory location.
it is based on the index value.it presents in java.util.Arrays package.
the main disadvantage of the array is size limitation.

2 types of array:-
  1. Single dimension array
  2. Multidimensional array.
Single Dimensional Array:-

Declaration;-  int []x=new int[3];
declare array of size 3.

Example:-

by default it takes zero.

Example 2:-


Example 3:- Length function in array.find the size of array.



x.length find the size of array.

Example 4:-declare and initialization in one line




Example 5:-Array.toString() method:- used to print the elements of array.



first SOP prints the address of the first element of the array.
second SOP prints the array.for printing the contents we use Array.toString() method.

Example 6:-Array.sort():-it sort the elements of array.



Example 7: -binarySearch():-it search the location of the elements presents in the array or not.
if the element is present it returns positive value otherwise it locates the location of the element in negative form.



20 present in list at first index so it give 0 and 65 is not present in the list so it will give the location in negative form.

Example 8:-Comparable:-it is used to sort array of derived data type or object type array.it is the interface so on which class you want to use Comparable write with implements keyword.



in this we take B type array to sort this array we use Comparable method.
in this we override string()method which return i value and compareTo() which takes object type argument so in this we downcasted the object type to B type by explicit downcasting and compare the value one by one.

Multidimensional Array:-it is used to represent 2D,3D arrays.

declarations:-int x[][]=new int[3][3];

Example:-


by default it takes 0

No comments :

Post a Comment