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

Arrays in Java

An array is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays have a fixed size that is set at initialization, and elements are accessed using indexes that start from 0 and go up to the array length minus one. Arrays are commonly used to store multiple values like student marks, and elements can be accessed individually or iterated through using a for loop.

Uploaded by

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

Arrays in Java

An array is a collection of similar data types stored in contiguous memory locations that can be accessed using an index. Arrays have a fixed size that is set at initialization, and elements are accessed using indexes that start from 0 and go up to the array length minus one. Arrays are commonly used to store multiple values like student marks, and elements can be accessed individually or iterated through using a for loop.

Uploaded by

Karan Rawat
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

TOPIC : ARR AY S I N J AVA

Introduction to Arrays
• An array is a collection of similar types of data having contiguous memory allocation.

• The indexing of the array starts from 0., i.e 1st element will be stored at the 0th index, 2nd element
at 1st index, 3rd at 2nd index, and so on.

• The size of the array can not be increased at run time therefore we can store only a fixed size of
elements in array.

• Use Case: Storing marks of 5 students


Accessing Array Elements :

Array elements can be accessed as follows,


So in a nut shell, this is how array works:

1. int[] marks; // Declaration!

marks = new int[5]; // Memory allocation!

2. int[] marks = new int[5]; // Declaration + Memory allocation!

3. int[] marks = {100,70,80,71,98} // Declare + Initialize!

Note : Array index start from 0 and go till (n-1) where n is the size of the array.
Looping Through Array output
Elements
class Main { Using for Loop:
public static void main(String[] args) { 12
4
// create an array 5
int[] age = {12, 4, 5};

// loop through the array


// using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}
}
}
Access Array output
Elements
class Main { Accessing Elements of Array:
public static void main(String[] args) { First Element: 12
Second Element: 4
// create an array Third Element: 5
int[] age = {12, 4, 5, 2, 5}; Fourth Element: 2
Fifth Element: 5
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
THANK YOU !
From :
Shivam Tyagi
Harsh Sharma
Karan Rawat

You might also like