LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • How to declare an array
  • Create an empty array
  • Accessing element of the array
  • Append a value to an array
  • Length/size of an array
  • In operator
  • Slicing an array
  • Exercises

Was this helpful?

  1. en
  2. examples
  3. section_3

Arrays

An array is a collection of items stored in contiguous memory locations. It's an aggregate data structure that is designed to store a group of objects of the same type. It's the most efficient data structure for storing and accessing a sequence of objects.

How to declare an array

Create an array that holds integer values:

mut ages := [18, 25, 37]

println(ages)

Output

[18, 25, 37]

Or create an array that holds string values:

mut users := ['vbrazo', 'donnisnoni95', 'Delta456']

println(users)

Output

['vbrazo', 'donnisnoni95', 'Delta456']

Note: All elements must have the same type. The following code will not compile.

mut users := ['vbrazo', 'donnisnoni95', 'Delta456', 0]

Output

~/main.v:2:43: bad array element type `int` instead of `string`

Create an empty array

If you want to create a new empty array, just declare [] followed by the data type.

mut names := []string
mut numbers := []int

Accessing element of the array

mut users := ['vbrazo', 'donnisnoni95', 'Delta456']

println(users[0])
println(users[2])
println(users[1])

Output

vbrazo
Delta456
donnisnoni95

Append a value to an array

<< is an operator that appends a value to the end of the array.

mut ages := [18]
ages << 47

println(ages)

Output

[18, 47]

It can also append an entire array.

mut ages := [18]
ages << [47, 49]

println(ages)

Output

[18, 47, 49]

Length/size of an array

.len method returns the length of the array.

mut names := ['Thiago', 'John']

println(names.len)

Output

2

In operator

in check if an element is inside an array.

mut names := ['Thiago', 'Alex', 'Joe']

println('Vitor' in names)
println('Thiago' in names)

Output

false
true

Slicing an array

It's easy to slice an array in V. You can slice an array with the default V slicing feature without having to call the slice () method. The syntax is like this my_array[start..end]

animals := ['lion', 'goose', 'chicken', 'turkey', 'tiger']
poultry := animals[1..4]
println(poultry) // ["goose", "chicken", "turkey"]

If you want to slice from the start of the index, just ignore it to put 0 and using instead my_array[..end] or my_array[start..].

x := ['h', 'e', 'l', 'l', 'o']
y := x[..x.len-1]
z := x[1..]
println(y) // ['h', 'e', 'l', 'l']
println(z) // ['e', 'l', 'l', '0']

Exercises

  1. Write a V program to store elements in an array and print it.

  2. Write a V program to read n number of values in an array and display it in reverse order.

  3. Write a V program to find the sum of all elements of the array.

  4. Write a V program to copy the elements of one array into another array.

  5. Write a V program to count a total number of duplicate elements in an array.

PreviousMethodsNextStruct

Last updated 5 years ago

Was this helpful?