Arry
Arry
©https://blogs.ashrithgn.com/
Objectives
• An array is a special type of container object which contains indexed collection of data
values of the same type.
• Arrays are created during runtime on the heap and follows the principle of the dynamic
allocation of data.
• The length of the array in Java cannot be increased or decreased after the array has
been created.
int number1 = 10; int [ ] numarray = {10, 20, 30, 40, 50};
int number2 = 20;
int number3 = 30;
int number4 = 40;
int number5 = 50; Multiple values of the
same types store under
a single name.
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Array Declaration & Creation
• Array Declaration:
• The square brackets indicate the array declaration.
<data type> [ ] <arrayname> //Type-1 (Preferred)
(OR)
<data type> <arrayname> [ ] // Type-2 (C-style declaration)
• Array Creation:
<arrayname> = new <data type> [<size>]
• Example:
double [ ] rainfall = new double [12];
• Fixed-size array:
double [ ] rainfall = new double[12];
• Variable-size array:
int size;
int [ ] number; //declare an array
System.out.print(“Enter size of an array: ");
size = scanner.nextInt();
number = new int[size]; //create an array
Faculty of Computer Science ©https://blogs.ashrithgn.com/
Array Initialization
• Like other data types, it is possible to declare and initialize an array at the same
time.
int [ ] number = { 1, 3, 5, 7 };
• An array has a public constant length variable for the size of an array.
number.length 4
String [ ] monthName = { "January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
double [ ] rainfall = new double[12];
double [ ] quarterAverage = new double[4];
monthName
0 1 2 3 4 5 6 7 8 9 10 11
January February March April May June July August September October November December
• Ch10RainfallStat.java
• How to use array of primitive data type arrays with sample program.