Skip to content

Commit 6abd970

Browse files
Introduction to Arrays
1 parent 33447ad commit 6abd970

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
1.55 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.company;
2+
3+
public class cwh_26_arrays {
4+
public static void main(String[] args) {
5+
/* Classroom of 500 students - You have to store marks of these 500 students
6+
You have 2 options:
7+
1. Create 500 variables
8+
2. Use Arrays (recommended)
9+
*/
10+
// There are three main ways to create an array in Java
11+
// 1. Declaration and memory allocation
12+
// int [] marks = new int[5];
13+
14+
// 2. Declaration and then memory allocation
15+
// int [] marks;
16+
// marks = new int[5];
17+
// Initialization
18+
// marks[0] = 100;
19+
// marks[1] = 60;
20+
// marks[2] = 70;
21+
// marks[3] = 90;
22+
// marks[4] = 86;
23+
24+
// 3. Declaration, memory allocation and initialization together
25+
int [] marks = {98, 45, 79, 99, 80};
26+
27+
// marks[5] = 96; - throws an error
28+
System.out.println(marks[4]);
29+
}
30+
}

0 commit comments

Comments
 (0)