Arrays

46 0 0
                                    

Arrays is like creating a variable but in multiple times.

=================================
creating a variable
int x = 1;

creating an array of 5 integers
int[] x = new int(5);
x[0] = 100;
x[1] = 200;
x[2] = 300;
x[3] = 400;
x[4] = 500;

≈===============================

1. First, we declared an array of integers to create 5 variables.

2  Next, we initialize each variable with a integer value.

Note:
All arrays start with index zero. That is why the first element is x[0]. So, the last element is n - 1 index. In the example, x[5 - 1].

In this example, we tried to initialize each element of the array one by one but in reality,  you can just used a loop like for loop which will be discuss later.

Another way to create an array is by combining the declaration and initialization.

===============================

int[] x = {100, 200, 300, 400, 500};

===============================

You can also create a multi dimensional array.

Two dimensions
============================≈=

int[][] x = {{1000, 2000, 3000}, {5000}};

=============================
Equivalent in array indexes

x[0][0] = 1000
x[1][0] = 2000
x[2][0] = 3000
x[0][1] = 5000

In math, using two dimensions in graph we usually used x and y.

variable[x][y]

Note:
Java has prepared several methods in the built in library. These methods and properties are usually the common ones we used in the program. Like for example the length, sorting, copying, converting to string and etc which can be found in java.util.Arrays class.

=========================≈=
System.out.println(x.length);
============================
Result:
5

Java ProgrammingWhere stories live. Discover now