- Need to specify size while declaration, size = total number of elements
- Element can be accessed using array "index"
- Array index starts from zero [0]
======================================================
Ex. 1: Array of int (primitive data type)
Source: TestArray1.java
public class TestArray1 {
public static void main(String[] args) {
int[ ] ilist = new int [10];
ilist[0] = 101;
ilist[7] = 200;
for(int k = 0; k < ilist.length; k++ ) {
System.out.println(ilist[k]);
}
}
}
Output:
101
0
0
0
0
0
0
200
0
0
======================================================
Ex. 2: Array of float (primitive data type)
Source: TestArray2.java
public class TestArray2 {
public static void main(String[] args) {
float[] ilist = new float [10]; // size
ilist[0] = 101.50;
ilist[7] = 200.25;
for(int k = 0; k < ilist.length; k++ ) {
System.out.println(ilist[k]);
}
}
}
Output:
101.50
0.0
0.0
0.0
0.0
0.0
0.0
200.25
0.0
0.0
======================================================
Ex. 3: Array of String (reference data type)
Source: TestArray3.java
public class TestArray3 {
public static void main(String[] args) {
String[] weekdays = new String[7]; // size
weekdays[0] = "Monday";
weekdays[1] = "Tuesday";
weekdays[2] = "Wednesday";
weekdays[3] = "Thursday";
weekdays[4] = "Friday";
weekdays[5] = "Saturday";
weekdays[6] = "Sunday";
for(int k = 0; k < weekdays.length; k++ ) {
System.out.println(weekdays[k]);
}
}
}
Output:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday