Смарт контракты

Published 15 October 2018

Arrays in Java

What is an array and what is it used for

What is an array and what is it used for

An array is a data structure that stores a set of values (array elements) that are identified by an index relative to the same data type.

The scope of arrays is extensive, they are used in loops to find the maximum and minimum values in the sequences of numbers. Using arrays simplifies the algorithms of search and calculation, and also reduces the number of declared variables.

Arrays in the Java language are referred to as reference types. To work with an array in Java, it must be declared, created, and initialized.

The array declaration begins by specifying the data type of the array to which the variable identifier will refer. The construct is as follows:

Array_type [] identifier_1, identifier_2, and so on;

int [] x,y;  or int x[]; String [] myList; double [] z;

After declaring an array variable, you define the array itself by using the new keyword, specifying the data type and size.

x=new int[20]; y=new int[100];  z=new double[100];

The number of elements in the array is specified in square brackets. All elements of the array are automatically assigned default values, and the initial value for numeric elements is 0.

You can initialize an array with its own values when you declare it and specify the number of elements.

int [] x={1,2,3,4,5,6,7,8,9,10};

An array has the length field – an array length, the first element of the array has an index of 0, the last index length-1.

One-dimensional and multidimensional arrays

One-dimensional and multidimensional arrays

Java uses one-dimensional and multidimensional arrays. A one-dimensional array is a collection of similar elements, which are accessed by a single index.

Figure 1. Structure of a one-dimensional array

 

A multidimensional array is an array of arrays. The most common of multidimensional arrays is a two-dimensional array. It represents a table with rows and columns. The number of its elements is determined by the specified number of rows and columns.

Figure 2. Structure of a two-dimensional array.

 

Declaration and creation of a two-dimensional array is shown in the following example:

int[][] tab1 = new int[3][3];

Creates a two-dimensional array tab1 of integers. The number of square brackets indicates the size of the array, numbers inside the brackets - the number of rows and columns.

Array elements are accessed using indexes, the first index is the line number, and the second index is the column number.

tab1[0][0]=9;

System.out.println(tab1[0][0]);

Initialization of a two-dimensional array with its own values is as follows:

int[][] y = { { 0, 1, 2 }, { 3, 4, 5 } };

Examples of using arrays

Examples of using arrays 

Search for the maximum positive element in a two-dimensional array

 

public class MyClass {

public static void main(String args[]) {

int tab1[][] = {{5, 46, -10},

                {-1, 200, 60},

                {-60, 50,-100}};

    int max=0;

    for (int i = 0; i < tab1.length; i++) {

        for (int j = 0; j < tab1[i].length; j++) {

            if (tab1[i][j] > 0 && tab1[i][j] > max) {

                    max = tab1[i][j];

                }

            }

        }

    System.out.println(max);

}

}

 

Search for the minimum negative element in a two-dimensional array

 

public class MyClass {

public static void main(String args[]) {

int tab1[][] = {{5, -460, -10},

                {-1, 200, 60},

                {-60, 50,-100}};

    int min=0;

    for (int i = 0; i < tab1.length; i++) {

        for (int j = 0; j < tab1[i].length; j++) {

            if (tab1[i][j]< 0 && tab1[i][j]<min) {

                    min = tab1[i][j];

                }

            }

        }

    System.out.println(min);

}

}

Votes 0, average rating 0

Contents