0% found this document useful (0 votes)
529 views15 pages

Arrays PDF

The document discusses arrays in Java. It defines arrays as fixed-size data structures that store homogeneous elements indexed by integers. The document provides examples of declaring, initializing, accessing, and manipulating array elements. It demonstrates how to find minimum/maximum values, copy arrays, and check for null elements in arrays. The key topics covered are the basic syntax and usage of arrays in Java programs.

Uploaded by

Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
529 views15 pages

Arrays PDF

The document discusses arrays in Java. It defines arrays as fixed-size data structures that store homogeneous elements indexed by integers. The document provides examples of declaring, initializing, accessing, and manipulating array elements. It demonstrates how to find minimum/maximum values, copy arrays, and check for null elements in arrays. The key topics covered are the basic syntax and usage of arrays in Java programs.

Uploaded by

Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

JAVA Means DURGASOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 1|Page
JAVA Means DURGASOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 2|Page
JAVA Means DURGASOFT

Arrays
 Arrays are used to represent group of elements as a single entity but these elements are
homogeneous &fixed size.
 The size of Array is fixed it means once we created Array it is not possible to increase and
decrease the size.
 Array in java is index based first element of the array stored at 0 index.
Advantages of array:-
 Instead of declaring individual variables we can declare group of elements by using array it
reduces length of the code.
 We can store the group of objects easily & we are able to retrieve the data easily.
 We can access the random elements present in the any location based on index.
 Array is able to hold reference variables of other types.

First element element at 5th position


10 20 30 40 50 60 70 80 90

0 1 2 3 4 5 6 7 8 index
Length is 9
Different ways to declare a Array:-
int[] values;
int []values;
int values[];
declaration& instantiation & initialization :-
Approach 1:- inta[]={10,20,30,40}; //declaring, instantiation, intialization
Approach 2:- int[] a=new int[100]; //declaring, instantiation
a[0]=10; //initialization
a[1]=20;
;;;;;;;;;;;;;;;;;;;;;;
a[99]=40;

// declares an array of integers


int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 10;
// initialize second element
anArray[1] = 20;

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 3|Page
JAVA Means DURGASOFT

// and so forth
anArray[2] = 30; anArray[3] = 40; anArray[4] = 50; anArray[5] = 60;
anArray[6] = 70; anArray[7] = 80; anArray[8] = 90; anArray[9] = 100;

Example :- taking array elements from dynamic input by using scanner class.
importjava.util.*;
class Test
{ public static void main(String[] args)
{ int[] a=new int[5];
Scanner s=new Scanner(System.in);
System.out.println("enter values");
for (int i=0;i<a.length;i++)
{ System.out.println("enter "+i+" value");
a[i]=s.nextInt();
}
for (int a1:a)
{ System.out.println(a1);
}
}
}

Example :- find the sum of the array elements.


class Test
{ public static void main(String[] args)
{ int[] a={10,20,30,40};
int sum=0;
for (int a1:a)
{ sum=sum+a1;

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 4|Page
JAVA Means DURGASOFT

}
System.out.println("Array Element sum is="+sum);
}
}

Method parameter is array & method return type is array:-


class Test
{ static void m1(int[] a) //method parameter is array
{ for (int a1:a)
{ System.out.println(a1);
}
}
staticint[] m2() //method return type is array
{ System.out.println("m1 method");
return new int[]{100,200,300};
}
public static void main(String[] args)
{ Test.m1(new int[]{10,20,30,40});
int[] x = Test.m2();
for (int x1:x)
{ System.out.println(x1);
}
}
}
Example:- adding the objects into Array and printing the objects.
class Test
{ public static void main(String[] args)
{ int[] a = new int[5];
a[0]=111;
for (int a1:a)
{ System.out.println(a1);
}
Emp e1 = new Emp(111,"ratan");
Emp e2 = new Emp(222,"anu");
Emp e3 = new Emp(333,"sravya");
Emp[] e = new Emp[5];
e[0]=e1;
e[1]=e2;
e[2]=e3;
for (Empee:e)
{ System.out.println(ee); }
}
}
Output:-
E:\>java Test
111 0 0 0 0
Emp@530daa Emp@a62fc3 Emp@89ae9e null null
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 5|Page
JAVA Means DURGASOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 6|Page
JAVA Means DURGASOFT

Example:- printing array elements with elements and default values.


class Test
{ public static void main(String[] args)
{ Emp[] e = new Emp[5];
e[0]=new Emp(111,"ratan");
e[1]=new Emp(222,"anu");
e[2]=new Emp(333,"sravya");
for (Object ee:e)
{ if (eeinstanceofEmp)
{ Empeee = (Emp)ee;
System.out.println(eee.eid+"----"+eee.ename);
}
if (ee==null)
{ System.out.println(ee);
}
}
}
}
Output:-
E:\>java Test
111----ratan
222----anu
333----sravya
null
null
Finding minimum & maximum element of the array:-
class Test
{ public static void main(String[] args)
{ int[] a = new int[]{10,20,5,70,4};
for (int a1:a)
{ System.out.println(a1);
}
//minimum element of the Array
int min=a[0];
for (int i=1;i<a.length;i++)
{ if (min>a[i])
{ min=a[i];
}
}
System.out.println("minimum value is ="+min);
//maximum element of the Array
int max=a[0];
for (int i=1;i<a.length;i++)
{ if (max<a[i])
{ max=a[i];
}
}
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 7|Page
JAVA Means DURGASOFT

System.out.println("maximum value is ="+max);


}
}

Example :- copy the data from one array to another array


class Test
{ public static void main(String[] args)
{ int[] copyfrom={10,20,30,40,50,60,70,80};
int[] copyto = new int[7];
System.arraycopy(copyfrom,1,copyto,0,7);
for (intcc:copyto)
{ System.out.println(cc);
}
}
}
Example :- copy the data from one array to another array
class Test
{ public static void main(String[] args)
{ int[] copyfrom={10,20,30,40,50,60,70,80};
int[] newarray=java.util.Arrays.copyOfRange(copyfrom,1,4);
for (intaa:newarray)
{ System.out.println(aa);//20 30 40
}
}
}

Example:- finding null index values.


class Test
{ public static void main(String[] args)
{ String[] str= new String[5];
str[0]="ratan";
str[1]="anu";
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 8|Page
JAVA Means DURGASOFT

str[2]=null;
str[3]="sravya";
str[4]=null;
for (int i=0;i<str.length;i++)
{ if ( str[i]==null)
{ System.out.println(i);
}
}
}
}
Root structure:-
java.lang.Object
|
|--java.lang.reflect.Array
Array is a final class can’t be extended.

To get the class name of the array:-


class Test
{ public static void main(String[] args)
{ int[] a={10,20,30};
System.out.println(a.getClass().getName());
}
}

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 9|Page
JAVA Means DURGASOFT

Example:-process of adding different types Objects in Object array


Test.java:-
class Test
{ public static void main(String[] args)
{ Object[] a= new Object[6];
a[0]=new Emp(111,"ratan");
a[1]=new Integer(10);
a[2]=new Student(1,"anu");
for (Object a1:a)
{ if (a1 instanceofEmp)
{ Emp e1 = (Emp)a1;
System.out.println(e1.eid+"---"+e1.ename);
}
if (a1 instanceof Student)
{ Student s1 = (Student)a1;
System.out.println(s1.sid+"---"+s1.sname);
}
if (a1 instanceof Integer)
{ System.out.println(a1);
}
if (a1==null)
{ System.out.println(a1);
}
}
}
}
Emp.java:
classEmp
{ inteid;
String ename;
Emp(inteid,Stringename)
{ //conversion of local to instance
this.eid=eid;
this.ename=ename;
}
}
Student.java:-
class Student
{ intsid;
String sname;
Student(intsid,Stringsname)
{ //conversion of local to instance
this.sid=sid;
this.sname=sname;
}
}
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 10 | P a g e
JAVA Means DURGASOFT

declaration of multi dimensional array:-


int[][] a;
int [][]a;
int a[][];
int []a[];
Example :-
class Test
{ public static void main(String[] args)
{ int[][] a={{10,20,30},{40,50,60}};
System.out.println(a[0][0]);//10
System.out.println(a[1][0]);//40
System.out.println(a[1][1]);//50
}
} 0 1

10 20 30 10 20 30
0 1 2 0 1 2
a[0][0]------10 a[0][1]------20 a[0][2]-----30 a[1][0]-----40
a[1][1]-----50 a[1][2]-----60
Example:-
class Test
{ public static void main(String[] args)
{ String[][] str={{"A.","B.","C."},{"ratan","ratan","ratan"}};
System.out.println(str[0][0]+str[1][0]);
System.out.println(str[0][1]+str[1][1]);
System.out.println(str[0][2]+str[1][2]);
}
}

Example :-febonacci series


importjava.util.Scanner;
class Test
{ public static void main(String[] args)
{ System.out.println("enter start series of febonacci");
int x = new Scanner(System.in).nextInt();
int[] feb = new int[x];
feb[0]=0;
feb[1]=1;
for (int i=2;i<x;i++)
{ feb[i]=feb[i-1]+feb[i-2];
}
//print the data
nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 11 | P a g e
JAVA Means DURGASOFT

for (int feb1 : feb)


{ System.out.print(" "+feb1);
}
}
}

Example :-febonacci series


importjava.util.Scanner;
class Test
{ public static void main(String[] args)
{ System.out.println("enter the no required for febonacci");
int a = new Scanner(System.in).nextInt();

System.out.println("enter first no of febonacci");


int x = new Scanner(System.in).nextInt();
System.out.println("enter second no of febonacci");
int y = new Scanner(System.in).nextInt();

int[] feb = new int[a];


feb[0]=x;
feb[1]=y;
for (int i=2;i<a;i++)
{ feb[i]=feb[i-1]+feb[i-2];
}
//print the data
for (int feb1 : feb)
{ System.out.print(" "+feb1);
}
}
}
Pre-increment & post increment :-
Pre-increment :- it increases the value by 1 then it will execute statement.
Post-increment :-it executes the statement then it will increase value by 1.

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 12 | P a g e
JAVA Means DURGASOFT

class Test
{ public static void main(String[] args)
{ //post increment
int a=10;
System.out.println(a); //10
System.out.println(a++); //10
System.out.println(a); //11
//pre increment
int b=20;
System.out.println(b); //20
System.out.println(++b); //21
System.out.println(b); //21
System.out.println(a++ + ++a + a++ + ++a);
//11 13 13 15
}
}
Pre-decrement &postdencrement :-
Pre-decrement :- it decreases the value by 1 then it will execute statement.
Post-decrement :-it executes the statement then it will increase value by 1.
class Test
{ public static void main(String[] args)
{ //post decrement
int a=10;
System.out.println(a); //10
System.out.println(a--); //10
System.out.println(a); //9
//post decrement
int b=20;
System.out.println(b); //20
System.out.println(--b); //19
System.out.println(b); //19
System.out.println(a-- + --a + a-- + --a);
//9 7 7 5
}
}

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 13 | P a g e
JAVA Means DURGASOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 14 | P a g e
JAVA Means DURGASOFT

nd
DURGASOFT, # 202,2 Floor,HUDAMaitrivanam,Ameerpet, Hyderabad - 500038,  040 – 64 51 27 86,
80 96 96 96 96, 9246212143 | www.durgasoft.com 15 | P a g e

You might also like