LoginSignup
1
0

More than 1 year has passed since last update.

配列とリストの基本操作(Unity)をまとめて行きます。

Last updated at Posted at 2021-06-09

配列とリストの基本操作(Unity)をまとめて行きます。

配列編

①宣言の仕方

        //長さが5の空の配列を宣言
        int[] numbers = new int[5];

        //長さが5の3,5,4,2,1を含む配列を宣言
        int[] numbers = new int[] { 3, 5, 4, 2, 1 };

        //これはダメ、バグになる
        int[] numbers = new int[5] { 3, 5};

②配列の長さを確認する方法

        //長さが5の空の配列を宣言
        int[] numbers = new int[5];
        Debug.Log(numbers.Length);//結果は5

③配列の要素を取り出す

        //長さが5の空の配列を宣言
        int[] numbers = new int[5];
        int number = numbers[3];
        Debug.Log(number);//結果はintのデフォ値0、参照型だと結果はnull

④配列の要素を書き込み

        //長さが5の空の配列を宣言
        int[] numbers = new int[5];
        numbers[3] = 4;
        Debug.Log(numbers[3]);//結果は4

⑤配列をリサイズ

        //長さが5の配列を宣言
        int[] numbers = new int[] { 3, 5, 4, 2, 1 };

        //長さ5の配列を長さ3にリサイズ、後ろの入りきれない要素の2と1はなくなる
        System.Array.Resize(ref numbers, 3);//{3, 5, 4}

        //長さ3の配列を長さ5にリサイズ、後ろに空の要素を二つ追加する、intだと0が追加される
        System.Array.Resize(ref numbers, 5);//{ 3, 5, 4, 0, 0 };

⑥配列の並べ替え

        //長さが5の配列を宣言
        int[] numbers = new int[] { 3, 5, 4, 2, 1 };

        //並べ替え、結果は{ 1, 2, 3, 4, 5 },floatの配列にも使える
        System.Array.Sort(numbers);

⑦配列から同じ要素を保持するリストを作る

        //長さが5の配列を宣言
        int[] numbers = new int[] { 3, 5, 4, 2, 1 };
        List<int> numList = new List<int>();
        numList.AddRange(numbers);

リスト編

①宣言の仕方

        //空のリストを宣言
        List<int> numbers = new List<int>();

        //空のリストだが、5要素分のメモリーを確保したリストを宣言
        List<int> numbers = new List<int>(5);

        //長さが5の3,5,4,2,1を含むリストを宣言
        List<int> numbers = new List<int> { 3, 5, 4, 2, 1 };

②リストの要素数とキャパシティーの確認の仕方

リストの要素数はCount(読み取り専用)、
確保したメモリー数はCapacity、こっちは書き込み可能だが、Countより小さい数字を指定するとバグる

        //空のリストだが、5要素分のメモリーを確保したリストを宣言
        List<int> numbers = new List<int>(5);
        Debug.Log(numbers.Count);//結果は0
        Debug.Log(numbers.Capacity);//結果は5
        //長さが5の3,5,4,2,1を含むリストを宣言
        List<int> numbers = new List<int> { 3, 5, 4, 2, 1 };
        Debug.Log(numbers.Count);//結果は5
        numbers.Capacity = 3;//5より小さいため、これはバグる

③リストの要素を取り出す、書き込み、配列と一緒

        //長さが5の3,5,4,2,1を含むリストを宣言
        List<int> numbers = new List<int> { 3, 5, 4, 2, 1 };
        Debug.Log(numbers[3]);//結果は2
        numbers[3] = 100;
        Debug.Log(numbers[3]);//結果は100

④リストに要素を追加、複数追加

リストには要素を最後尾に追加するAddと、要素を複数まとめて最後尾に追加するAddRangeがある

        List<int> numbers = new List<int>();
        numbers.Add(3);
        int[] addNumbers = new int[]{4,5,6};
        numbers.AddRange(addNumbers);
        Debug.Log(numbers.Count);//結果は4//リストの中身は{3,4,5,6}

⑤リストから要素を削除

リストには配列にはない、特定の要素を削除するRemoveと、指定番号の要素を削除するRemoveAtがある。
Removeは同じ要素が複数あっても、一番左にある一つだけを削除する。
RemoveもRemoveAtも空欄を残さず、右の要素は左に一つシフトして空欄を埋める

        List<int> numbers = new List<int> {1,2,3,3,5 };
        numbers.Remove(3);//3を削除、リストの中身は{1,2,3,5}になる
        numbers.RemoveAt(3);//リストの中身は{1,2,3}になる

⑥リストに要素を挿入

        List<int> numbers = new List<int> {1,2,3,3,5 };
        numbers.Insert(3,9);//3番目に9を挿入する
        //リストの中身は{1,2,3,9,3,5}に

⑦リストに特定要素があるかどうか

        List<int> numbers = new List<int> {1,2,3,3,5 };
        Debug.Log(numbers.Contains(6));//結果はfalse
        Debug.Log(numbers.Contains(5));//結果はtrue

⑧リスト要素の並べ替え

        List<int> numbers = new List<int> {3,2,4,5,1 };
        numbers.Sort();//リストの中身が{1,2,3,4,5}に

⑨リストから同じ要素を持つ配列を作る

        List<int> numbers = new List<int> {3,2,4,5,1 };
        int[] numArray = numbers.ToArray();

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0