0% found this document useful (0 votes)
8 views6 pages

Arrays

Java PPT in Simple Way
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Arrays

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

Arrays

Arrays in Java
• An array is a collection of similar types of data.
• Array size is mandatory while creating arrays.
• Array is also known as static data structure because size of an array
must be specified at the time of its declaration.
For example, if we want to store the names of 100 people then we can
create an array of the string type that can store 100 names.

int[] array = new int[100];


Here, the above array cannot store more than 100 names. The number of
values in a Java array is always fixed.
How to declare an array in Java?
we can declare an array in java as
dataType[] arrayName;
dataType - it can be primitive data types like int, char, double, byte,
etc. or Java objects
arrayName - it is an identifier
For example,
double[] data; // declare an array
Here, data is an array that can hold values of type double.
data = new double[10]; // allocate memory

In Java, we can declare and allocate the memory of an array in one single
statement. For example,
How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration.
For example,
int[] age = {12, 4, 5, 2, 5}; //declare and initialize and array
Here, we have created an array named age and initialized it with the values inside
the curly brackets.

We can also initialize arrays in Java, using the index number. For example,
int[] age = new int[5]; // declare an array
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
..
How to Access Elements of an Array in Java?
We can access the element of an array using the index number. Here is
the syntax for accessing elements of an array,

array[index] // access array elements


We can use loops to access all the elements of the array at once.
Multidimensional Arrays
A multidimensional array is an array of arrays. That is, each element of
a multidimensional array is an array itself. For example,

double[][] matrix = {{1.2, 4.3, 4.0},


{4.1, -1.1}
};
Here, we have created a multidimensional array named matrix.
It is a 2-dimensional array.

You might also like