Java Arrays


Java Arrays

Arrays are used to store multiple values ​​for a single variable, instead of declaring different values ​​for each value.

To declare the same members, define a variable type with square brackets:


String[] cars;
        

We have now announced the variables that hold the string of strings. To add numbers to it, we can use the same physical components - put values ​​in a comma-separated list, within folded parentheses:


String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
        

To create whole numbers, you can type:


int[] myNum = {10, 20, 30, 40};
        


Access the Elements of an Array

You are accessing an element of the same members by referring to the reference number.

This statement covers the value of the first element in cars:


Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            System.out.println(cars[0]);
            // Outputs Volvo
            

Note: Index lists start with the 0: [0] first item. [1] second element, etc.



Change an Array Element

To change the value of a specific element, see the reference number:


Example
cars[0] = "Opel";
        

Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            cars[0] = "Opel";
            System.out.println(cars[0]);
            // Now outputs Opel instead of Volvo
            


Array Length

To find out how many features of the same member, use the length feature:


Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            System.out.println(cars.length);
            // Outputs 4
            


Loop Through an Array

You can enter between elements of the same loop with a for loop, and use the length feature to specify how many loops should work.

The following example removes all elements from the array of cars:


Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            for (int i = 0; i < cars.length; i++) {
              System.out.println(cars[i]);
            }
            


Loop Through an Array with For-Each

There is also a "for-one" loop, which is specially used to combine elements into identical members:


Syntax
for (type variable : arrayname) {
            ...
          }
          

The following example removes all features from the array of cars, using the "for-each" loop:


Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
            for (String i : cars) {
              System.out.println(i);
            }
            

The example above can be read as follows: for each String element (pronounced as i in index) on cars, print the value of i.

If you compare the loop with each loop, you will see that each method is easy to write, does not require a counter (using a length structure), and is very readable.


Multidimensional Arrays

A multidimensional array is a group of identical members.

To create identical two-dimensional members, add the same individual members within their set of curly brackets:


Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
        

myNumbers is now a program with two identical members as their components.

To access myNumbers array features, specify two indicators: one for the program, and one for the component within that list. This example reaches the third (2) element in the second (1) program of my numbers:


Example
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
            int x = myNumbers[1][2];
            System.out.println(x); // Outputs 7
            

We can also use a for loop inside other for loop to get a loop to get the features of a two-dimensional system (we still have to point to two directions):


Example
public class Main {
            public static void main(String[] args) {
              int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
              for (int i = 0; i < myNumbers.length; ++i) {
                for(int j = 0; j < myNumbers[i].length; ++j) {
                  System.out.println(myNumbers[i][j]);
                }
} } }