Arrays. Part-1. One-dimensional arrays

Arrays. One-dimensional arrays


 


1. What is an array? What is the purpose of the arrays in program

Array – a set (group) of identical elements, united by the same name. The arrays allow convenient to group the information and gain access to it.

Arrays can be one-dimensional, two-dimensional and multi-dimensional.

Position of the element in the array is called the index. In Java, the position of the first element in the array starts from zero.

The array considered the one-dimensional, if to determine the location of element in the array need to specify the value of one index.

The array considered the two-dimensional, if to determine the location of element in the array need to specify the value of two indexes.

 

2. What types of data can take the elements of an array?

 Array elements can be of any type, allowable in language Java.

This type defines the data type of any of the elements that make up the array.

Detailed description of the primitive data types can be found here.

 

3. What is the general form of the description of one-dimensional array?

The general form of the description of one-dimensional array:

type variable_name[];

or

type[] variable_name;

where

  • type – is the array element type, which is also called the base type;
  • variable_name – the array name, that will access the array elements.

Then you need to allocate memory for the array. The general form of memory allocation is as follows:

variable_name = new type[size];

where

  • type – base type of array items;
  • size – the count of array items.

Allocating memory for the array elements can be carried out immediately after it’s definition. In this case, the general form of the description of the array has the form:

type variable_name[] = new type[size];

or

type[] variable_name = new type[size];

When allocating memory the items of array are filled:

  • zero values for numeric types;
  • false values for type boolean;
  • empty values null for reference types.

So, to create an array the process takes place in two stages:

  • declare a variable of the desired type of the array;
  • using the new operator is allocated memory for the array elements.

 

4. Example of creating and using a one-dimensional array with the name of d, in which there are 5 elements of type int.

 

// description of the array named d in which the items are of type int
int d[];
d = new int[5]; // memory allocation for the 5 items of type int of array d

// access to the items of array d
d[2] = -8; // in the 3rd element of the array write the number -8
d[0] = -1000;
d[4] = 29;
d[5] = 300; // error - ArrayIndexOutOfBoundsException: 5

In this example, the creation of array can be done in different ways.

Way 1.

int d[];
d = new int[5];

Way 2.

int[] d;
d = new int[5];

Way 3.

int[] d = new int[5];

Way 4.

int d[] = new int[5];

 

5. Example of creating and using a one-dimensional array f, which contains 10 elements of type float.

 

float[] f; // array named f, the array items are of type float
f = new float[10]; // memory allocation for the 10 items of the array f

f[0] = 0.0f;
f[1] = 1.0f;
f[2] = 2.0f;
f[3] = 3f;
f[4] = 4.0f;
f[5] = 5.0f;

 

6. Example of creating and using of a one-dimensional array x, in which 20 items are of type double.

 

// the description of array x of 20 items of type "double"
double x[] = new double[20];

x[8] = -3.3998;
x[3] = -5.5; // x[3] = -5.5
x[3] = x[7]; // x[3] = 0.0

 

7. Sample of code snippet of zeroing one-dimensional array named x, which contains 20 numbers of double type.

In this example is zeroed an array of real numbers of type “double“.

// the definition of array x, which contains 20 elements of type double
double x[] = new double[20];

for (int i=0; i<x.length; i++)
    x[i] = 0.0;

 

8. Example of calculating the sum of a one-dimensional array of M items, where there are 50 real numbers.

 

// calculation the sum of array items
float[] M = new float[50];
float sum = 0; // the required sum

// filling the data of array
// ...

// the loop of sum calculation
for (int i=0; i<M.length; i++)
    sum = sum + M[i];

 

9. Example of finding a maximum value in array of 100 integers

 

// searching of maximum value
int[] A = new int[100]; // array declaration
int max; // the required value

// filling the data of array
// ...

// the maximum value search loop
max = A[0];
for (int i=1; i<A.length; i++)
    if (max < A[i])
        max = A[i];

 

10. Example of sorting of one-dimensional array of real numbers by the “bubble” method.

 

float M[] = new float[5]; // declaration of the array of 5 elements
float x; // auxiliary variable

// inputting of array M, the filling of values
// ...

// the loop of sorting by the "bubble" method
for (int i=0; i<M.length-1; i++)
    for (int j=i; j>=0; j--)
        if (M[j]>M[j+1])
        {
            x = M[j];
            M[j] = M[j+1];
            M[j+1] = x;
        }

 

11. How in Java implement the assignment of one array to another? Method clone().

For deep copying of arrays is used clone() method from the Java library.

Example of assignment one array into another with the use of clone() method.

float[] f; // array named f, the array elements are of type float
float f2[];
f = new float[5]; // memory allocation for the 5 items in the array f
f2 = new float[5]; // memory allocation for the array f2

f[0] = 0.0f;
f[1] = 1.0f;
f[2] = 2.0f;
f[3] = 3f;
f[4] = 4.0f;

// method clone() - deep copying
f2 = f.clone(); // arrays f and f2 - are different arrays
f[2]=3.8f; // f2[2] = 3.8; f[2] = 2.0

If you write:

f2 = f;

both variables f and f2 will point to the same memory area.

 

12. How is realized the initialization of one-dimensional array?

In Java programs, you can initialize arrays during their declaration. In this case, is specified a list of expressions that are separated by a comma ‘,’. This list of expressions is taken in braces.

Examples of initializing of arrays of different dimensions and different types of items.

// initialization of a one-dimensional array of 5 numbers of type double
double M[] = { 3.8, -2.55, 0.23, -11.45, 3.85 };

// initialization of a one-dimensional array of 5 numbers of type byte
byte[] B = { -1, 25, 44, -12, 14 };

// initialization of the array of type char
char C[] = { 'a', 'b', 'c', 'd', '5', ';', '9', 'N', '!', '$' };

 

13. Example of a one-dimensional array of classes.

Let there be given a class named MyPoint. It is necessary to describe the object-variable, which is an array of 3 classes MyPoint.

// declaration of the MyPoint class
class MyPoint
{
    int x; // internal variable x
    int y; // internal variable y

    // class methods
    public void SetXY(int xx, int yy)
    {
        x = xx;
        y = yy;
    }

    public int GetX()
    { return x; }

    public int GetY()
    { return y; }
}

...

// using of array of classes in the program
// declaration of the variable of type the array of classes MyPoint
MyPoint[] mp;

// memory allocation for the array of classes
mp = new MyPoint[10];

// memory allocation for each class
for (int i=0; i<mp.length; i++)
    mp[i] = new MyPoint();

mp[0].SetXY(10, 20);
mp[1].SetXY(30, -100);
mp[2].SetXY(4, -345);

int d;
d = mp[2].GetY(); // d = -345
d = mp[0].GetX(); // d = 10
d = mp[1].GetY(); // d = -100

 

14. Example of declaring a one-dimensional array of interfaces.

Let there be given a class named MyPoint. Class implements the MyInterface interface.

It is necessary to describe the object-variable, which is an array of 5 elements of type interface MyInterface.

// declaration the MyInterface interface
interface MyInterface
{
    void SetXY(int xx, int yy);
}

// declaration of MyPoint class
class MyPoint implements MyInterface
{
    int x; // internal variable x
    int y; // internal variable y

    // interface method implementation
    public void SetXY(int xx, int yy)
    {
        x = xx;
        y = yy;
    }
 
    // class methods
    public int GetX()
    { return x; }

    public int GetY()
    { return y; }
}

...

// declaration of the variable of type the array of MyPoint classes
MyPoint[] mp;

// memory allocation for the array of classes
mp = new MyPoint[3];
// memory allocation for every class
for (int i=0; i<mp.length; i++)
    mp[i] = new MyPoint();

mp[2].SetXY(4, -345); // filling of values of the class members

int d = mp[2].GetY(); // d = -345

// declaration of the variable mi of type the array of MyInterface interfaces
MyInterface[] mi;

// memory allocation for an array of references to interfaces
mi = new MyInterface[5];

// memory allocation for each variable-interface
for (int i=0; i<mi.length; i++)
    mi[i] = new MyPoint();

// using of variable mi
mi[0] = mp[2];       // mi[0] refers to mp[2]
mi[0].SetXY(23, 35); // change the mi [0] - means to change mp [2]
d = mp[2].GetY();   // d = 35

 

15. Sample of defining and using of a one-dimensional array of strings

In the Java language to work with strings is introduced a special class String. This example demonstrates the operations:

  • declaration of string array and its initialization;
  • copying of the array of strings into another array;
  • using of clone() method to copy arrays of strings.
// Declaring and initializing of an array of strings
String[] M1 =   {
                    "Text #1",
                    "Text #2",
                    "Text #3"
                };

// declaration the array M2 and assignment it to array M1
String[] M2;
M2 = new String[3];

// assign one array to another
for (int i=0; i<3; i++)
    M2[i] = M1[i];

// the assignment of arrays with the use of clone() method
String[] M3;
M3 = M1.clone();