facebook twitter hatena line email

「Unity/Csharp/配列」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(順番逆順に)
(リストとリストを結合する)
 
(同じ利用者による、間の31版が非表示)
行18: 行18:
  
 
==クラスリスト==
 
==クラスリスト==
 +
クラス例
 +
<pre>
 +
class Car
 +
{
 +
    public string name;
 +
    public int value;
 +
    public Car(string name, int value)
 +
    {
 +
        this.name = name;
 +
        this.value = value;
 +
    }
 +
}
 +
</pre>
 +
クラスリスト例
 
  List<Car> cars = new List<Car>();
 
  List<Car> cars = new List<Car>();
 
  cars.Add( new Car("Lexas", 50));
 
  cars.Add( new Car("Lexas", 50));
行29: 行43:
 
  Car car = cars["Lexas"];
 
  Car car = cars["Lexas"];
  
==List<string>に新規で追加==
+
==ディクショナリをenumキーで==
 +
Dictionary<HogeType, Button> enumDict = new Dictionary<HogeType, Button>();
 +
 
 +
==ディクショナリforeach==
 +
foreach(KeyValuePair<HogeType, Button> pair in enumDict)
 +
{
 +
  Debug.Log (pair.Key + " " + pair.Value);
 +
}
 +
 
 +
==ディクショナリのキーの存在判定==
 +
<pre>
 +
if (cars.ContainsKey("Lexas")) {
 +
    // 存在するとき
 +
}
 +
</pre>
 +
 
 +
==TryGetValueを使うとき==
 +
<pre>
 +
Car carvalue = null;
 +
cars.TryGetValue("Lexas", out carvalue);
 +
</pre>
 +
参考:https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.dictionary-2.trygetvalue?view=net-5.0
 +
 
 +
==Listに新規で追加==
 
  List<string> names = new List<string> { "taro", "jiro", "saburo" };
 
  List<string> names = new List<string> { "taro", "jiro", "saburo" };
 +
List<int> nums = new List<int>() { 0, 1, 2 };
  
 
==List<string>からstring[]へ==
 
==List<string>からstring[]へ==
 
  List<string> words = new List<string>();
 
  List<string> words = new List<string>();
  words.Add(“taro”);
+
  words.Add("taro");
  words.Add(“jiro”);
+
  words.Add("jiro");
  words.Add(“saburo”);
+
  words.Add("saburo");
 
  string[] wordStrings = words.ToArray();
 
  string[] wordStrings = words.ToArray();
  
行49: 行87:
 
  string name = list[2]; // 3 - 1
 
  string name = list[2]; // 3 - 1
  
==先頭に追加
+
==先頭に追加==
 
  List<string> names = new List<string>();
 
  List<string> names = new List<string>();
 
  names.Insert(0, "taro");
 
  names.Insert(0, "taro");
行57: 行95:
 
  nums.Reverse(); // 参照渡しとなり戻り値はvoid
 
  nums.Reverse(); // 参照渡しとなり戻り値はvoid
  
==配列に文字数字が含まれてるか確認==
+
==配列に文字や数字が含まれてるか確認==
 +
// 配列に文字が含まれてるか
 
  List<string> names = new List<string>();
 
  List<string> names = new List<string>();
 
  if (!names.Contains(name)) {
 
  if (!names.Contains(name)) {
 
  }
 
  }
  
 +
// 配列に数字が含まれてるか
 
  List<int> nums = new List<int>();
 
  List<int> nums = new List<int>();
 
  if (!nums.Contains(num)) {
 
  if (!nums.Contains(num)) {
 
  }
 
  }
 +
 +
==配列シャッフル==
 +
<pre>
 +
public static List<int> Shuffle(List<int> list)
 +
{
 +
        for (int i = list.Count - 1; i > 0; i--)
 +
        {
 +
            int j = Random.Range(0, i + 1);
 +
            var tmp = list[i];
 +
            list[i] = list[j];
 +
            list[j] = tmp;
 +
        }
 +
        return list;
 +
}
 +
</pre>
 +
参考:https://qiita.com/o8que/items/bf07f824b3093e78d97a
 +
 +
==検索==
 +
<pre>
 +
string[] src = {"taro", "jiro", "saburo"};
 +
var list = new List<string>();
 +
list.AddRange(src);
 +
string item = "jiro";
 +
int num = list.IndexOf(item); // キー番号取得(ノーマッチは-1で、マッチしたキー番号は0~)
 +
if (num > -1) {
 +
    Debug.Log("ari");
 +
}
 +
</pre>
 +
 +
==配列の最初から削除==
 +
<pre>
 +
List<string> hoges = new List<string>();
 +
hoges.Add("taro");
 +
hoges.Add("jiro");
 +
hoges.Add("saburo");
 +
for (int a = 0; a <= 2; a++)
 +
{
 +
    Debug.Log("hoge=" + hoges[0]);
 +
    hoges.Remove(hoges[0]);
 +
}
 +
Debug.Log("count=" + hoges.Count);
 +
</pre>
 +
出力
 +
<pre>
 +
hoge=taro
 +
hoge=jiro
 +
hoge=saburo
 +
count=0
 +
</pre>
 +
 +
==配列から指定する値を削除==
 +
hoges.Remove("taro");
 +
 +
==配列をindex指定で削除==
 +
hoges.RemoveAt(0);
 +
 +
==配列をindex指定から幅で削除==
 +
hoges.RemoveRange(0, 10);
 +
第1引数がindexで、第2引数は件数
 +
 +
 +
<pre>
 +
var hoges = new List<int>();
 +
hoges.Add(10);
 +
hoges.Add(11);
 +
hoges.Add(12);
 +
hoges.Add(13);
 +
hoges.RemoveRange(1, 3);
 +
hoges.Add(20);
 +
hoges.Add(21);
 +
hoges.Add(22);
 +
for (int i = 0; i < 10; i++)
 +
{
 +
    Debug.Log("i=" + i + " " + hoges[i]);
 +
}
 +
</pre>
 +
出力例
 +
<pre>
 +
i=0 10
 +
i=1 20
 +
i=2 21
 +
i=3 22
 +
</pre>
 +
 +
==配列を,区切りで文字列に==
 +
using System;
 +
List<string> words = new List<string>();
 +
words.Add("taro");
 +
words.Add("jiro");
 +
words.Add("saburo");
 +
string[] wordStrings = words.ToArray();
 +
Debug.Log(String.Join(",", wordStrings)); // taro,jiro,saburo
 +
 +
==文字列を,区切りで配列に==
 +
<pre>
 +
string wordStr = "taro,jiro,saburo";
 +
string[] words =  wordStr.Split(',');
 +
</pre>
 +
 +
==リストとリストを結合する==
 +
mergeする
 +
<pre>
 +
var list = new List<int>{1 ,2, 3};
 +
var list2 = new List<int>{4 ,5, 6};
 +
list.AddRange(list2);
 +
// listに1,2,3,4,5,6が入る
 +
</pre>
  
 
==参考==
 
==参考==
 
公式リストとディクショナリ
 
公式リストとディクショナリ
 
https://unity3d.com/jp/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries
 
https://unity3d.com/jp/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

2024年2月3日 (土) 13:52時点における最新版

配列宣言

int[] nums;
float[] f;
Vector3[] v;
GameObject[] gameObjects;

数字配列宣言&入力

int[] nums = {10,20,30,40,50,60,70,80,90,100};

or

int[] nums = new int[10];
nums[0] = 10;
nums[1] = 20;
Debug.Log(nums[1]); // 20

文字列配列宣言&入力

string[] names = new string[] { "taro", "jiro", "saburo" };
Debug.Log(names[1]); // jiro

クラスリスト

クラス例

class Car
{
    public string name;
    public int value;
    public Car(string name, int value)
    {
        this.name = name;
        this.value = value;
    }
}

クラスリスト例

List<Car> cars = new List<Car>();
cars.Add( new Car("Lexas", 50));

クラスディクショナリ

Dictionary<string, Car> cars = new Dictionary<string, Car>();
Car bg1 = new Car("Lexas", 50);
Car bg2 = new Car("Rx8", 100);
cars.Add("Lexas", bg1);
cars.Add("Rx8", bg2);
Car car = cars["Lexas"];

ディクショナリをenumキーで

Dictionary<HogeType, Button> enumDict = new Dictionary<HogeType, Button>();

ディクショナリforeach

foreach(KeyValuePair<HogeType, Button> pair in enumDict)
{
 Debug.Log (pair.Key + " " + pair.Value);
}

ディクショナリのキーの存在判定

if (cars.ContainsKey("Lexas")) {
    // 存在するとき
}

TryGetValueを使うとき

Car carvalue = null;
cars.TryGetValue("Lexas", out carvalue);

参考:https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.generic.dictionary-2.trygetvalue?view=net-5.0

Listに新規で追加

List<string> names = new List<string> { "taro", "jiro", "saburo" };
List<int> nums = new List<int>() { 0, 1, 2 };

List<string>からstring[]へ

List<string> words = new List<string>();
words.Add("taro");
words.Add("jiro");
words.Add("saburo");
string[] wordStrings = words.ToArray();

string[]からList<string>へ

string[] wordStrings = {"taro","jiro","saburo"};
List<string> list = new List<string>(wordStrings);

Listの3番目に入れる

list.InsertRange(3, "saburo");

Listの3番目のデータ取得

string name = list[2]; // 3 - 1

先頭に追加

List<string> names = new List<string>();
names.Insert(0, "taro");

順番逆順に

int[] nums = new int[] { 1, 2, 3, 4 };
nums.Reverse(); // 参照渡しとなり戻り値はvoid

配列に文字や数字が含まれてるか確認

// 配列に文字が含まれてるか
List<string> names = new List<string>();
if (!names.Contains(name)) {
}
// 配列に数字が含まれてるか
List<int> nums = new List<int>();
if (!nums.Contains(num)) {
}

配列シャッフル

public static List<int> Shuffle(List<int> list)
{
        for (int i = list.Count - 1; i > 0; i--)
        {
            int j = Random.Range(0, i + 1);
            var tmp = list[i];
            list[i] = list[j];
            list[j] = tmp;
        }
        return list;
}

参考:https://qiita.com/o8que/items/bf07f824b3093e78d97a

検索

string[] src = {"taro", "jiro", "saburo"};
var list = new List<string>();
list.AddRange(src);
string item = "jiro";
int num = list.IndexOf(item); // キー番号取得(ノーマッチは-1で、マッチしたキー番号は0~)
if (num > -1) {
    Debug.Log("ari");
}

配列の最初から削除

List<string> hoges = new List<string>();
hoges.Add("taro");
hoges.Add("jiro");
hoges.Add("saburo");
for (int a = 0; a <= 2; a++)
{
    Debug.Log("hoge=" + hoges[0]);
    hoges.Remove(hoges[0]);
}
Debug.Log("count=" + hoges.Count);

出力

hoge=taro
hoge=jiro
hoge=saburo
count=0

配列から指定する値を削除

hoges.Remove("taro");

配列をindex指定で削除

hoges.RemoveAt(0);

配列をindex指定から幅で削除

hoges.RemoveRange(0, 10);

第1引数がindexで、第2引数は件数

var hoges = new List<int>();
hoges.Add(10);
hoges.Add(11);
hoges.Add(12);
hoges.Add(13);
hoges.RemoveRange(1, 3);
hoges.Add(20);
hoges.Add(21);
hoges.Add(22);
for (int i = 0; i < 10; i++)
{
    Debug.Log("i=" + i + " " + hoges[i]);
}

出力例

i=0 10
i=1 20
i=2 21
i=3 22

配列を,区切りで文字列に

using System;
List<string> words = new List<string>();
words.Add("taro");
words.Add("jiro");
words.Add("saburo");
string[] wordStrings = words.ToArray();
Debug.Log(String.Join(",", wordStrings)); // taro,jiro,saburo

文字列を,区切りで配列に

string wordStr = "taro,jiro,saburo";
string[] words =  wordStr.Split(',');

リストとリストを結合する

mergeする

var list = new List<int>{1 ,2, 3};
var list2 = new List<int>{4 ,5, 6};
list.AddRange(list2);
// listに1,2,3,4,5,6が入る

参考

公式リストとディクショナリ https://unity3d.com/jp/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries