Você está na página 1de 9

Aiiays

}SL Tech www.jsltech.com Page

Arrays:
O An array is an object that is a named set oI variables oI the same type. Each variable in
the array is called an array element.
O All elements in the array must be oI the same data type
O Arrays in Java are objects and can be oI primitive data types or reIerence variable type
To reIerence a particular element in an array, you use the array name combined with an integer
value oI type int, called an index.
int marks|6|new int||50,80,85,95.80};
Example marks |4|
Here marks is an array and |4| indicates the 4 element oI array
50 80 85 90 95 80
marks|0| marks|1| marks|2| marks|3| marks|4| marks|5|
The index value does not need to be an integer literal. It can be any expression that results in a
value oI type int that is equal to or greater than zero.
arr|(lowhigh)/2|
An Array index starts with 0 and ends with the size-1
0.aration of th0 array
int|| marksnew int|5|;
String|| namesnew String|5 |;
Student|| namenew Student|5|;
Note: Array size shouldn't be negative number, iI speciIy negative number it leads to the
runtime exception. java.lang.NegativeArraySizeException
Initiization of th0 array
int|| numbersnew int||1,2,3,4,5,6}
Here the integer array is created with numbers and size with 6 and array will be initilized
with 1,2,3,4,5,6 values.
int|| num10,20,30,40,60,70};
Here the integer array is created with name num and initialized with 10,20,30,40,60,70
L0ngth of an array
The number oI elements contains in the array can be Iind using length. Here length is Iield
Aiiays

}SL Tech www.jsltech.com Page

Examp0:
int|| anew int||1,5,10,15};
int lena.length (which returns the length is 4 )
Storing Array 00m0nts
We can store the elements into the array by using index or by running the Ior loop
Example
int|| anew int|3|
a|0|10;
a|1|20;
a|2|30;
(or)
Ior(int i0;ia.length;i)
a|i|i;
}
ArrayE0m0nts .an b0 a..0ss0/:
int a||new int|3|;
System.out.print(a|0|);
System.out.print(a|1|);
System.out.print(a|2|);
(or)
Ior(int i0;ia.length;i)
System.out.print(a|i|);
}
Array 00m0nts .an b0 pro.0ss0/ by using th0 for-0a.h oops
Ior(int i : a)
System.out.print(i);
In java Arrays are created dynamic memory, that is allocated at runtime by the JVM.
Aiiays

}SL Tech www.jsltech.com Page

int|| a;
Here just we created reIerence variable a and its not holding any object later we can initialize this
a reIerence variable with integer array object.
int|| anew int||10,20,30,40};
int|| ba;
int|| input||new int||1,2,3,4};
int|| outputnew int|4|;
int|| temp;
When you want to switch the array reIerenced by output array to be the new input array, we can
write
tempinput;
inputoutput;
outputtemp;
Examp0:
public class ArrayDemo
public static void main(String|| args)
int|| inputnew int||1,2,3,4};
int|| outputnew int|4|;
int|| temp;
Ior(int i0;iinput.length;i)
output|i|input|i|*5;
}
tempinput;
inputoutput;
outputtemp;
Ior(int i0;iinput.length;i)
System.out.println(output|i|);
} }
Aiiays

}SL Tech www.jsltech.com Page

uti/im0nsiona Arrays:
Multidimensional arrays are implemented as arrays oI arrays.
Multidimensional arrays are declared by appending the appropriate number oI bracket pairs aIter
the array name.
Declaration
int a||||new int|3||3|
Initialization oI the multidimensional array can be done in this way
int a||||new int|||| 2,3,4},3,4,5},4,5,6}};
Anonymous Arrays
Sometimes we can declare an array without name also such type oI arrays are called anonymous
arrays. Normally these arrays will be created Ior temporary usage.
/* program to Iind the biggest oI the Iour numbers*/
Example:
import java.util.Scanner;
public class ArraysDemo
public static void main(String|| args)
int a,b,c,d,big;
System.out.println("Enter the a,b,c");
Scanner scnew Scanner(System.in);
asc.nextInt();
bsc.nextInt();
csc.nextInt();
dsc.nextInt();
bigbiggestElement(new int||a,b,c,d});
System.out.println("The biggest element is : "big);
}private static int biggestElement(int|| a)
int big;
biga|0|;
Aiiays

}SL Tech www.jsltech.com Page

Ior(int i1;ia.length;i)
iI(a|i|~big)
biga|i|;

}
return big;
}
}
/*Program to Accept N number Irom the user,create an array with size N and accpt the numbers
and storing into array. Find the biggest and smallest elements int the given array */
package com.jsl.array;
import java.util.Scanner;
public class BiggestAndSmallest
public static void main(String|| args)
int big,small,array||;
Scanner scnew Scanner(System.in);
System.out.println("Enter the size oI the array ");
/* Creating new Array with the user given size */
arraynew int|sc.nextInt()|;
Ior(int i0;iarray.length;i)
System.out.println("Enter the element "(i1)" value ");
array|i|sc.nextInt();
}
/* initilizing big and small elements with the initial values */
bigarray|0|;
smallarray|0|;
/* Accessing array elements by using Ior-each loop */
Aiiays

}SL Tech www.jsltech.com Page

Ior(int ele:array)
iI(ele~big)
bigele;
iI(elesmall)
smallele;
}
System.out.println("The biggest element is :"big);
System.out.println("The smallest element is :"small);
}
}
/*program to search the element in the given array */
package com.jsl.array;
import java.util.Scanner;
public class SearchingElement
public static void main(String|| args)
int|| arrnew int|10|;
int ele;
Scanner scnew Scanner(System.in);
Ior(int i0;iarr.length;i)
System.out.print("Enter the value oI "(i1)" value ");
arr|i|sc.nextInt(); }
System.out.println("Enter the element to search ");
elesc.nextInt();
int posserarchElement(arr,ele);
iI(pos-1)
System.out.println("The element is not Iound ");
else
Aiiays

}SL Tech www.jsltech.com Page

System.out.println("The element is Iound at loaction : " pos);


}
private static int serarchElement(int|| arr, int ele)
int pos-1;
Ior(int i0;iarr.length;i)
iI(arr|i|ele)
posi;
break;
}
}
return pos;
}
}
/*Program to Iind the result,grade,average marks oI given students marks list*/
package com.jsl.arrays;
public class ArrayDemo
public static void main(String|| args)
int marks||||new int||||
23,45,56,34},
45,23,67,78},
78, 89,90,80},
53,45,78,61}
};
Ior(int i0;imarks.length;i)
int sum0;
Iloat aveg0.0I;
String result"Pass";
Aiiays

}SL Tech www.jsltech.com Page 8

String grade" ";



System.out.println("\n The student "(i1)" Details are \n");
System.out.println("Sub1\tSub2\tSub3\t Sub4 \t Total\t Average\tResult\tGrade");
Ior(int j0;jmarks|i|.length;j)
System.out.print(" "marks|i||j|"\t");
iI(marks|i||j|35)
result"Fail";
sumsummarks|i||j|;
}
avegsum/4;
iI(result.equals("Pass"))
iI(aveg~70)
grade"Distinction";
else iI(aveg~60 && aveg70)
grade"First Class";
else iI(aveg60)
grade"Second Class";
else
grade"Third Class";

}
else
grade" Sorry Study Well";
}
System.out.print(sum" \t "aveg"\t \t"result"\t "grade);
System.out.println("\n");
Aiiays

}SL Tech www.jsltech.com Page 9

}
}
}
Task:
1. Write a program to accept N value and create the array size N. Find the sum oI elements in the
array.
2. Write a program to accept N value and create the array size N. Find the biggest and the
smallest elements in the array
3. Write a program that allows you to create an integer array oI 18 elements with the Iollowing
values: int A||3,2,4,5,6,4,5,7,3,2,3,4,7,2,0,0,0,}. The program computes the sum oI element 0
to 14 and stores it at element 15, computes the average and stores it at element 16 and identiIies
the smallest value Irom the array and stores it at element 17.
4. Write a program that accepts two numbers in the range Irom 1 to 50 Irom the user. It then
compares these numbers against the single dimension array oI 5 integer elements ranging in
value Irom 1 to 50. The program displays the message success iI the two inputted values are
Iound in the array element. Otherwise print it as Iail.
5. Write a program that accepts M x N matrix and
1. Find the sum oI elements in the matrix
2. Biggest element in the matrix
3. Diagonal sum oI the matrix
4. Transpose oI the given matrix

Você também pode gostar