arraysbyksreddy

Array:
1)introduction
2)Array Decleration
3)Array construction
4)initilization
5)Decleration,construction and initilization in a single line
6)length vs length()
7)Anananymous Arrays
8)MultiDimesnional arrays
——————————–
A array is an indexed collection of fixed number of homogeneous data elements.
Arrays are dynamically created objects in Java code.
An array can hold a number of elements of the same type
The elements can be primitives or object references
Array index begins from zero.
An array can even contain other arrays ie Multidimensional arrays.
java supports single dimensional and multidimensional arrays.

developing a array is a 3 step process.
a)declaring the array
b)constructing the array
c)initialization.
————————————–
Single dimensional arrays:
–>Declaration
int x[];
or
int []x;
or
int[] x;//industry recommended(readability)

At the time of decleration we should not specify the size.
if we specify the size it is compile time error.
[]can be prefixed or suffixed

int x[5]; //CTE
String s[3]; //CTE

Object o[];
String s[];
Student s1[];
Employee e[];
————————————————-
construction of the array:
memory is allocated at the time of construction ie in the heap .
we should specify the size.
int x[];——————–1]
x= new int[3];————2]
or
int x[]=new int[3];
we use the new operator to construct an array
x is a reference to the array.
x contains the hashcode of the object.
array index begins from zero.
array contains default values.
byte–0
int—0
double—0.0
String—-null

In java it is legal to have an array with size zero.

int [] x= new int [0];// valid

——————————————
int []x=new int[3];
System.out.println(x[0]);//0
System.out.println(x[3]);//RTE ArrayIndexOutOfBoundsException
System.out.println(x[-1]);//RTE ArrayIndexOutOfBoundsException

——————————————
int []x= new int [-1];//RTE NegativeArraySizeException
———————————————–
int x[]=new int[2];
System.out.println(x[1]);//0
System.out.println(x[10.5]);//CTE
CTE :possible lossy conversion from int to double

————————————————
int x[]= new int[10.5F]; // CTE
int x[]= new int[(int)10.5F];//valid

The only allowed datatypes to mention various size of array are byte,short,int ,char because all can be converted to int
If we are providing any other datatype we will get compile time error.
The max allowed array size in java is size of int value.

int []x= new int[2147483647];//max number of int
int []y= new int[2147483648];//CTE integer number too large

int []x= new int[3L];//CTE

————————————————
int x[]= new int[‘A’];//65
int x[]= new int[“A”];//CTE

————————————————
length:—
it is the implicit variable and declared as final
it is applicable only on array objects.
Arrayname.length gives the size of the array.

int a[]= new int[6];
System.out.println(a.length);// 6
System.out.println(a.length());//CTE

int a[]= new int[‘A’];
System.out.println(a.length);//65

int a[]= new int[(int)10.5F];
System.out.println(a.length);//10

2)length()
length() is the final method applicable for String objects
It represents the no of characters present in the string
String s=”ksreddy”;
System.out.println(s.length);//CTE
System.out.println(s.length());//7
length is on arrays where as length() is on strings

———————————————–
initialization:
int x[]=new int[3];
x[0]=10;
x[1]=20;
x[2]=30;
—————————-
decleartion,construction,initialization in a single line.

int x[]={10,20,30};
int x[3]={10,20,30};//CTE
String s[]={“java”,”.net”,”PHP”};

—————————————–
float x[]={10.5F,20.5F,30.5};//CTE
int x[]={‘A’,’B’,10,20,30};//
——————————————-

refer the material for programs on arrays
———————————————–
for-each loop
it is introduced in 1.5 version of java to retrieve all the elements from the array.

int x[]={10,20,30};
for(int i:x)
System.out.println(i);

String s[]={“ram”,”rom”,”balm”};
for(String i:s)
System.out.println(i);
to compile a program with lower version

ex]javac -source 1.4 Demo2.java

——————————————-
class ArrayDemo1
{
public static void main(String rags[])
{
int x[]= new int [5];
x[0]=10;
x[1]=20;
x[2]=30;
x[3]=40;
x[4]=50;
System.out.println(x.length);//5
System.out.println(“normal for loop”);

for(int i=0;i<x.length;i++)
System.out.println(x[i]);

System.out.println(“for-each loop”);
for(int i:x)
System.out.println(i);

System.out.println(” for-each loop using float”);
float m[]={10.5F,20.5F,30.5F};
for(float u:m)
System.out.println(u);

System.out.println(” for-each loop using characters”);
char ch[]={‘A’,’B’,’C’,’D’};
for(char ch1:ch)
System.out.println(ch1);
System.out.println(“for-each loop using characters”);
char ch2[]={‘A’,’B’,’C’,’D’};
for(int i:ch2)
System.out.println(i);

System.out.println(” for-each loop using strings”);
String s1[]={“rama”,”sita”,”tina”,”dina”};
for(String s2:s1)
System.out.println(s2);
}}

javac -source 1.4 ArrayDemo1.java

————————————–
A array can have more than one reference.
class ArrayDemo2
{
public static void main(String rags[])
{
int x[]={100,200,300};
int y[]=x;
for(int i:x)
System.out.println(i);
System.out.println(“——–“);
for(int m:y)
System.out.println(m);
}}
—————————————–
copying the elements from one array to another array

public class ArrayDemo3
{
public static void main(String args[])
{
int x[]={10,20,30,40,50,60};
int y[]={100,200,300,400,500,600,700,800};
System.arraycopy(x,1,y,2,3);
for( int i=0; i < y.length; i++)
{
System.out.print(y[i] + ” “);
}}}

System.arraycopy(source array,position in the source array,destination array,position in the destination array,number of elements to copy)

System is a class from java.lang
arraycopy() is a static method to copy the array elements from source array to destination array

—————————————————–
Arrays are passed by reference.
in java objects are passed by using pass by reference but pass by reference uses pass by value.

———————————-
Anonymous Arrays:
A array not having the name is called anonmyous array
anonmyous arrays are instantly created ,used and destroyed.
While constructing anonymous arrays we are not allowed to specify the size.violation leads to compile time error.
new int[]{10,20,30}

class ArrayDemo4
{
static int sum(int x[])
{
int s=0; //initially sum is zero
for(int i:x)
s=s+i;

return s;
}
public static void main(String[] args)
{
System.out.println(ArrayDemo4.sum(new int[]{10,20,30}));
System.out.println(ArrayDemo4.sum(new int[]{40,50,60}));
System.out.println(ArrayDemo4.sum(new int[]{‘A’,’B’,’C’}));
}
}

———————————-
Arrays Class:
The java.util.Arrays class contains various static methods for sorting and searching arrays,comparing arrays, and filling array elements.

int x[]={10,5,3,12,15,6,1};
Arrays.sort(x);

String[] fruits = new String[] {“Pineapple”,”Apple”,”Orange”,”Banana”};
Arrays.sort(fruits);

import java.util.*;
class ArrayDemo5
{
public static void main(String[] args)
{
int x[]={10,5,3,12,15,6,1};
Arrays.sort(x);

for(int i:x)
System.out.println(i);

String[] fruits = new String[] {“Pineapple”,”Apple”,”Orange”,”Banana”};
Arrays.sort(fruits);

for(String s:fruits)
System.out.println(s);
}
}
—————————————-
command line arguments:
the data passed from the command line to the program is called command line arguments.
command line arguments are in string format and are stored in String args[] of main method.
args is the array name and it can be any name

class ArrayDemo6
{
public static void main(String[] args)
{
System.out.println(args.length);
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(“——————–“);
for(String s:args)
System.out.println(s);
}
}
D:\>java ArrayDemo6 hi how are you

class ArrayDemo7
{
public static void main(String[] args)
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[0]+args[1]);
//System.out.println(args[0]*args[1]); CTE
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
System.out.println(x+y);
}
}

D:\>java ArrayDemo7 10 20
——————————————
Multidimensional arrays:
array of arrays
ex]
two dimensional arrays
three dimensional arrays
———————————————–
two dimensional arrays
declearation:
int x[][];
int [][]x;
int[][] x;
int []x[];

String s[][];
float y[][];
byte b[2][3]; //CTE
——————————————-
construction of two dimensional array
int x[][];
x=new int[2][3];
or
int x[][]=new int[2][3];
arrayname.length returns first dimension value
ex] x.length—>2
x[0].length—3
x[1].length—3

int x[][]= new int[2][]; //valid
int x[][]= new int[][3]; //invalid
specifying the first dimension is mandatory.
—————————–
int x[][]= new int[2][];
x[0]=new int[5];
x[1]=new int[3];

In java there is no wastage of memory.

x.length—->2
x[0].length—-5
x[1].length—–3
—————————————
int x[][]=new int[2][];
x[0]= new int[1];
x[1]= null;

—————————————-
which of the following are valid

int x[][]= new int [‘A’][‘B’]; //valid
int x[][]= new int [“A”][“B”]; //invalid
int x[][]= new int [‘+’][‘?’]; //valid
int x[][]= new int [10.5][20.5]; //invalid
int x[][]= new int [(int)10.5][(int)20.5];//valid
———————————————-
initialization:
int x[][]= new int[2][2];
x[0][0]=10;
x[0][1]=20;
x[1][0]=30;
x[1][1]=40;
——————————————————
declration,construction,initialization in a single line
int x[][]={{10,20,30},
{40,50,60,70,80},
{100,200}};
—————————————–
three dimensional arrays

declearation:
int x[][][];
int [][][]x;
int[][][] x;
int []x[][];
int [][]x[];

construction
int x[][][]=new int[1][2][3];
intialization
int x[][][]=new int[1][2][3];
x[0][0][0]=10;
x[0][0][1]=20;
x[0][0][2]=30;
x[0][1][0]=40;
x[0][1][1]=50;
x[0][1][2]=60;
1)int[][][] a= new int[3][4][5]; // valid
2)int [][][]a= new int[3][][];//valid
3)int [][][]a= new int[3][4][];//valid
4)int [][][]a= new int[3][][5];// invalid
5)int [][][]a= new int[][3][5];// invalid

note:– Array of arrays is multidimensional arrays: