0% found this document useful (0 votes)
14 views

Java Array

Java arrays allow storing multiple values of the same type in a single variable. Arrays are declared with a type name followed by square brackets, and their size is fixed after declaration. Elements within an array are accessed using an index with the first element having an index of 0. Loops can be used to iterate through and access all elements of an array sequentially in Java.

Uploaded by

Marlowe Marquez
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Array

Java arrays allow storing multiple values of the same type in a single variable. Arrays are declared with a type name followed by square brackets, and their size is fixed after declaration. Elements within an array are accessed using an index with the first element having an index of 0. Loops can be used to iterate through and access all elements of an array sequentially in Java.

Uploaded by

Marlowe Marquez
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA ARRAYS

Java Arrays
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables
for each value.

• 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.

• 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?
• In Java, here is how we can declare an array.

• dataType - it can be primitive data types like


int, char, double, byte, etc. or Java objects
• arrayName - it is an identifier
How to declare an array in Java?
For example,
double[] data;
• Here, data is an array that can hold values of
type double.

But, how many elements can array this hold?


• To define the number of elements that an array
can hold, we have to allocate memory for the
array in Java.
How to declare an array in Java?
Example:

• Here, the array can store 10 elements. We can also say


that the size or length of the array is 10.

• In Java, we can declare and allocate the memory


of an array in one single statement.
How to Initialize Arrays in Java?
In Java, we can initialize arrays during declaration.

• Here, we have created an array named age and


initialized it with the values inside the curly brackets.
• Note that we have not provided the size of the array. In
this case, the Java compiler automatically specifies the
size by counting the number of elements in the array (i.e.
5).
How to Initialize Arrays in Java?
In the Java array, each memory location is associated with a
number. The number is known as an array index. We can also
initialize arrays in Java, using the index number.

NOTE:
• Array indices always start from 0. That is, the first element of
an array is at index 0.
• If the size of an array is n, then the last element of the array
will be at index n-1.
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,
Example: Access Array Elements

• In the above example, notice that we are using the index


number to access each element of the array.
Looping Through Array Elements
• In Java, we can also loop through each element of the array.

• In the above example, we are using the for Loop in Java to


iterate through each element of the array. Notice the
expression inside the loop,

• Here, we are using the length property of the array to get the size of
the array.

You might also like