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