Kotlin. Class Array. The purpose. Arrays of data

Class Array. The purpose. Arrays of data. Ways to create the array

1. The concept of array. Class Array. Ways to create the array

In the Kotlin language, arrays are represented by the Array class. To declare an array, you must specify its type as a parameter between a pair of <> characters.

var ArrayName : Array<T>

here

  • ArrayName – the name of array;
  • T – type of array elements (Int, Double, String, Char and others).

To create an array of type Array<T>, you need to use one of the following methods:

  • call the arrayOf() method. This method creates an array with elements of the specified type;
  • use the constructor of Array class based on the specified lambda expression;
  • use the arrayOfNulls() method. This method creates an array of the given size where all elements are null.

 

2. Ways to create the array of type Array
2.1. Using the arrayOf() Method

The arrayOf() method creates an array of the specified type. In the most general case, using a method in a program to create an array can have several implementations.

// Create the array based on a list of elements
val arrName1 = arrayOf(vararg elements : T)
val arrName3 = arrayOf(vararg elements : T)

// Create empty array
var arrName2 = arrayOf()
var arrName4 = arrayOf()

here

  • arrName1, arrName2, arrName3, arrName4 – names of created arrays;
  • T – the type of array items;
  • elements – list of array elements separated by ‘ , ‘ (comma) character.

If no parameters are specified when calling the arrayOf() method, then an empty array is created. The keywords var and val refer to the variables arrName1, arrName2, arrName3, arrName4. The keyword val implies a one-time creation of an array associated with a variable. The var keyword specifies that the array can be re-created, e.g.

// Declaring an array as a var array
var arrayName : Array<T> = ...

// Reusing the arrayName for an array
arrayName = ...

 

2.1.1. Creating and filling an array of numbers of type Int

 

// Creating an array. Method arrayOf()
// Create empty array of integers and fill it
// 1. Declare a variable of type Array<Int>
var AI : Array<Int>

// 2. Create empty array
AI = arrayOf()

// 3. Fill array with numbers: [ 2, 4, 6, ..., 20 ]
for (i in IntRange(2, 20) step 2)
  AI += i

// 4. Display the array
for (i in AI)
  print(i.toString() + " ")

 

2.1.2. Creating and filling the array of strings of type String

 

This example uses the string fill form directly in the arrayOf() method.

 

// Creating the array. Method arrayOf()
// 1.Create the array of strings that contains the names of the days of the week
var AS : Array<String> = arrayOf("Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thirsday",
  "Friday",
  "Saturday"
)

// 2. Display the names of the days of the week in a columnfor (s in AS)
println(s)

 

2.1.3. Creating and filling the array of numbers of Double type

The example uses the implementation of the arrayOf() method that receives a list of array values as a parameter.

 

// Creating the array. Method arrayOf()
// 1. Create the array of numbers of Double type
var AD : Array<Double> = arrayOf(2.8, 3.5, -1.4, 9.2)

// 2. Display array AD
for (x in AD)
  print(x.toString() + " ")

 

2.1.4. Creating the array of characters of type Char

The example creates the array AC containing a list of characters representing a number in hexadecimal:

  • digits ‘0’‘9’;
  • letters of the Latin alphabet ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’.

 

// Create the array. Method arrayOf()
// 1. Create the array of characters of type Char:
//   [ '0', '1', '2', ..., '9', 'A', 'B', 'C', 'D', 'E', 'F' ]
var AC : Array<Char> = arrayOf()

// 2. Create the array
// 2.1. Add digits
for (i in IntRange(0, 9))
  AC += ('0' + i).toChar()

// 2.2. Add letters 'A', ..., 'F'
for (c in CharRange('A', 'F'))
  AC += c

// 3. Display the array
for (c in AC)
  print(c + " ")

 

2.2. Using the Array constructor

The Array class constructor receives a lambda expression as a parameter. This lambda expression fills the elements of an array with values.

In the most general case, creating an array of the Array class using the constructor looks like this:

var ArrayName = Array<T>(size) { lambda_expression }

here

  • ArrayName – the name of the created array;
  • T – the type of the array elements;
  • size – the number of elements (size) in the array;
  • lambda_expression – lambda expression that generates the values of the array elements.

You can use the val keyword before declaring the array

val ArrayName = Array<T>(size) { lambda_expression }

which will mean that the array variable ArrayName cannot be used to create a new array. Attempt to re-create a new array

ArrayName = ...

will produce a compilation error.

 

2.2.1. Form the array of numbers of Double type

 

// Using the constructor of the Array class.
// 1. Create the array of squares of numbers from 0 to 9 of type Double.
//   When creating an array, a lambda expression is used
var AD = Array<Double>(10) { i -> (i*i).toDouble() }

// 2. Display the array
for (i in AD)
  print(i.toString() + " ")
println()

 

2.2.2. Form the array of strings of type String

 

// Using the Array class constructor for the String type
// 1. Form the array of empty strings
val Days = Array<String>(7) { "" }

// 2. Write the value to the array
Days[0] = "Sunday"
Days[1] = "Monday"
Days[2] = "Tuesday"
Days[3] = "Wednesday"
Days[4] = "Thirsday"
Days[5] = "Friday"
Days[6] = "Saturday"

// 3. Display the array of strings
for (day in Days)
  print(day + " ")

 

2.2.3. Form the array of Boolean values

 

// Using the Array class constructor for the String type
// 1. Form the array of Boolean values.
//    Weekends are true in the array
var DayOff = Array<Boolean>(7) { false }

// 2. Set weekends
DayOff[5] = true
DayOff[6] = true

// 3. Display the array
for (d in DayOff)
  print(d.toString() + " ")

 

2.2.4. Form the array of characters of type Char

When forming an array of characters of the Char type, a lambda expression is used.

// Create a character array containing uppercase Latin letters
var AC = Array<Char>(26) { c -> ('A' + c).toChar() }

// Display the array
for (i in AC)
  print(i + " ")

 

2.3. Using the method arrayOfNulls()

 

The arrayOfNulls() method allows you to create an array of values of a given type and a given size, which can contain a null value. The general form of declaring an array using the arrayOfNulls() method is as follows

var arrayName : Array<T> = arrayOfNulls(size)

here

  • arrayName – the name of array to be created;
  • T – the type of the array elements;
  • size – the size of the array that is reserved (allocated).

After an array is declared, its elements are null. The declaration assumes that the zero values of the array elements will be further changed.

 

2.3.1. Creating the array of numbers of type Short

An array of numbers of type Short is created. Each number specifies the number of days in the corresponding month of the year whose number is the array index plus 1.

// Method arrayOfNulls() - creating an array containing a null value.
// A list of days in each month is formed.

// 1. Create the array of 12 elements of type Short
val DayInMonths : Array<Short?> = arrayOfNulls(12)

// 2. Write to the array the data of months in which there are 31 days,
//   use when statement
for (i in IntRange(0, DayInMonths.size-1))
  DayInMonths[i] = when (i+1) {
    2 -> 28
    4, 6, 9, 11 -> 30
    else -> 31
  }

// 3. Display the array DayInMonths
for (d in DayInMonths)
  print(d.toString() + " ") // 31 28 31 30 31 30 31 31 30 31 30 31

 

2.3.2. Creating an array of constant values of type Float

 

// Method arrayOfNulls() - creating the array containing null values
// 1. Create the array of values of type Float
val Squares : Array<Float?>
Squares = arrayOfNulls(5)

// 2. Fill array with values
var i : Int = 0

while (i < Squares.size) {
  Squares[i] = (i*i).toFloat()
  i++
}

// 3. Display the array Squares
for (x in Squares)
  print(x.toString() + " ")

 

2.3.3. Creating the array of strings of type String

 

// Method arrayOfNulls() - creating the array containing a null value
// 1. Create the array of strings
var Seasons : Array<String?> = arrayOfNulls<String>(4)

// 2. Fill array of strings with values
Seasons[0] = "Winter"
Seasons[1] = "Spring"
Seasons[2] = "Summer"
Seasons[3] = "Autumn"

// 3. Display the array of strings
for (s in Seasons)
  print(s + " ")