Array Usage in Java

Itacen Sabacok | Jul 1, 2021
 1int[] numeric = {1, 2, 3, 4};
 2String[] carBrands = {"audi", "bmw", "mercedes", "porsche"};
 3
 4// Outputs 3
 5System.out.println(numeric[2]);
 6
 7// Outputs bmw
 8System.out.println(carBrands[1]);
 9
10// array will be {1, 200, 3, 4}
11numeric[1] = 200;
12
13// array will be {"audi", "bmw", "mercedes", "ferrari"}
14carBrands[3] = "ferrari"

Array Loop

 1int[] numeric = {1, 2, 3, 4};
 2String[] carBrands = {"audi", "bmw", "mercedes", "porsche"};
 3
 4// for loop with array
 5for (int i = 0; i < numeric.length; i++) {
 6  System.out.println(numeric[i]);
 7}
 8
 9// foreach loop with array
10for (String i : carBrands) {
11  System.out.println(i);
12}