Arrays Tutorials
- 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
Different ways to Declare a Array
- int[] values;
- int []values;
- int values[];
Declaration & Instantiation & Intialization
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
// 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.
1: importjava.util.*;2: class Test
3: { public static void main(String[] args)4: { int[] a=new int[5];6: System.out.println("enter values");5: Scanner s=new Scanner(System.in);8: { System.out.println("enter "+i+" value");7: for (int i=0;i<a.length;i++) 9: a[i]=s.nextInt(); 10: }15: }11: for (int a1:a) 12: { System.out.println(a1); 13: }14: }
Example :-find the sum of the array elements..
1: class Test2: {
3: public static void main(String[] args)4: { 5: int[] a={10,20,30,40};9: sum=sum+a1;6: int sum=0; 7: for (int a1:a) 8: {11: System.out.println("Array Element sum is="+sum);10: } 12: }13: }
Example:- Method parameter is array & method return type is array
1: class Test2: {
3: static void m1(int[] a) //method parameter is array4: { 5: for (int a1:a) 6: {10: staticint[] m2() //method return type is array7: System.out.println(a1); 8: } 9: } 11: {13: return new int[]{100,200,300};12: System.out.println("m1 method"); 14: } 15: public static void main(String[] args) 16: {21: System.out.println(x1);17: Test.m1(new int[]{10,20,30,40}); 18: int[] x = Test.m2(); 19: for (int x1:x) 20: { 22: } 23: }24: }
Example:- Adding the objects into Array and printing the objects.
1: class Test2: {
3: public static void main(String[] args)4: { 5: int[] a = new int[5];8: {6: a[0]=111; 7: for (int a1:a)11: Emp e1 = new Emp(111,"pritam");9: System.out.println(a1); 10: }14: Emp[] e = new Emp[5];12: Emp e2 = new Emp(222,"anu"); 13: Emp e3 = new Emp(333,"xyz");19: {15: e[0]=e1; 16: e[1]=e2; 17: e[2]=e3; 18: for (Empee:e)23: }20: System.out.println(ee); 21: }22: }
Example :- Copy the data from one array to another array
1: class Test2: {
3: public static void main(String[] args)4: {6: int[] copyto = new int[7];5: int[] copyfrom={10,20,30,40,50,60,70,80};8: for (intcc:copyto)7: System.arraycopy(copyfrom,1,copyto,0,7); 9: { 10: System.out.println(cc);13: }11: }12: }
Example:- Finding null index values.
1: class Test2: {
3: public static void main(String[] args)4: { 5: String[] str= new String[5];8: str[2]=null;6: str[0]="ratan"; 7: str[1]="anu"; 9: str[3]="sravya";12: {10: str[4]=null; 11: for (int i=0;i<str.length;i++) 13: if ( str[i]==null)19: }14: { 15: System.out.println(i); 16: } 17: }18: }
Declaration of multi dimensional array:-
- int[][] a;
- int [][]a;
- int a[][];
- int []a[];
Example :-
1: class Test2: {
3: public static void main(String[] args)4: { int[][] a={{10,20,30},{40,50,60}};6: System.out.println(a[1][0]);//405: System.out.println(a[0][0]);//10 7: System.out.println(a[1][1]);//509: }8: }
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.
1: class Test2: {
3: public static void main(String[] args)4: { 5: //post increment7: System.out.println(a); //106: int a=10; 8: System.out.println(a++); //1010: //pre increment9: System.out.println(a); //11 11: int b=20; 12: System.out.println(b); //2015: System.out.println(a++ + ++a + a++ + ++a);13: System.out.println(++b); //21 14: System.out.println(b); //21 16: / /11 13 13 15 17: }18: }
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.
1: class Test2: {
3: public static void main(String[] args)4: { 5: //post decrement7: System.out.println(a); //106: int a=10;9: System.out.println(a); //98: System.out.println(a--); //1012: System.out.println(b); //2010: //post decrement 11: int b=20;14: System.out.println(b); //1913: System.out.println(--b); //1918: }15: System.out.println(a-- + --a + a-- + --a); 16: //9 7 7 517: }
Comments
Post a Comment