facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(string[]からListへ)
(順番逆順に)
(同じ利用者による、間の7版が非表示)
行28: 行28:
 
  cars.Add("Rx8", bg2);
 
  cars.Add("Rx8", bg2);
 
  Car car = cars["Lexas"];
 
  Car car = cars["Lexas"];
 +
 +
==List<string>に新規で追加==
 +
List<string> names = new List<string> { "taro", "jiro", "saburo" };
  
 
==List<string>からstring[]へ==
 
==List<string>からstring[]へ==
行42: 行45:
 
==Listの3番目に入れる==
 
==Listの3番目に入れる==
 
  list.InsertRange(3, "saburo");
 
  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)) {
 +
}
  
 
==参考==
 
==参考==
 
公式リストとディクショナリ
 
公式リストとディクショナリ
 
https://unity3d.com/jp/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries
 
https://unity3d.com/jp/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

2019年3月21日 (木) 23:31時点における版

配列宣言

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

クラスリスト

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"];

List<string>に新規で追加

List<string> names = new List<string> { "taro", "jiro", "saburo" };

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)) {
}

参考

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