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
  • 配列を宣言する方法
  • 空の配列を作成する
  • 配列の要素にアクセスする
  • 配列の末尾に値を追加する
  • 配列の長さ
  • in演算子
  • 配列をスライスする
  • 演習

Was this helpful?

  1. コード例で学ぶV言語
  2. examples
  3. section_3

配列

配列(array)は、メモリ上の連続した位置に保存された項目のコレクションです。配列は、型が同じであるオブジェクトのグループを保存するよう設計された、集約的なデータ構造であり、オブジェクトのシーケンスを保存したりアクセスしたりする場合に最も効率の高いデータ構造です。

配列を宣言する方法

要素の型がintegerの配列を1つ作成する:

mut ages := [18, 25, 37]

println(ages)

上の出力結果:

[18, 25, 37]

要素の型がstringaの配列を1つ作成する:

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

println(users)

上の出力結果:

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

注意: どの要素もすべて同じ型でなければなりません。以下はコンパイルされません。

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

上の出力結果:

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

空の配列を作成する

宣言で[]に続けてデータ型を書くことで、空の配列を1つ作成できます。

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

配列の要素にアクセスする

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

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

上の出力結果:

vbrazo
Delta456
donnisnoni95

配列の末尾に値を追加する

<<は、配列の末尾に値をひとつ追加する演算子です。

mut ages := [18]
ages << 47

println(ages)

上の出力結果:

[18, 47]

値の代わりに配列を追加することもできます。

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

println(ages)

上の出力結果

[18, 47, 49]

配列の長さ

.lenメソッドは配列の長さ(つまり要素の数)を返します。

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

println(names.len)

上の出力結果

2

in演算子

inは、要素が配列にあるかどうかをチェックします。

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

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

上の出力結果

false
true

配列をスライスする

Vでは配列を簡単にスライス(切り出し)できます。slice ()メソッドを呼ばなくても、Vのデフォルトのスライシング機能だけで配列をスライスできます。 構文は配列[開始値..終了値]のようになります。

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

スライスをインデックスの冒頭から開始したい場合、開始値に0を置かずに配列[..終了値]と書くことも、終了値に-1を置かずに配列[開始値..]と書くこともできます。

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', 'o']

演習

  1. 要素をいくつか渡すと配列に保存して出力するVプログラムを書きましょう。

  2. 配列のn番目の値を読み取って逆順で表示するVプログラムを書きましょう。

  3. 配列のすべての要素の合計を求めるVプログラムを書きましょう。

  4. ある配列の要素を別の配列にコピーするVプログラムを書きましょう。

  5. 配列の中で重複している要素の個数を数えるVプログラムを書きましょう。

PreviousメソッドNext構造体

Last updated 5 years ago

Was this helpful?