1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【スタジオしまづ】>Udemy >セ5:C#基礎>配列基礎

1
Last updated at Posted at 2020-03-25

教材番号19 配列の基礎と応用

あの配列と親しくなりたいシリーズ

・変数をいっぱい作るのが大変な時、配列を使います。
 int、int、int、int、int、int、な人とか変われる。

・宣言、取得、更新を大事に!

  (宣言)まずはここから

//パターン1)
        int[] xList = new int[3]; 
        xList[0] = 0;
        xList[1] = 2;
        xList[2] = -3;

//パターン2)
        int[] yList = new int[3]{0,2,-3};

 (取得) できるかも
// 値の取得
        Debug.Log(xList[0]);
        Debug.Log(xList[1]);
        Debug.Log(xList[2]);    

// ながさ取得(要素の数)
        Debug.Log(xList.Length);

  (for文との連携de出力)ついに?
        for(int i=0; i<xList.Length; i++)
        {
            Debug.Log(xList[i]);
        }

素人なのでfor文の「i=0」って言われても「i」が分からなかったりしました。
別に「i」じゃなくてもいいんですよ。多分「coi」でも。。

演習 0から30までの数を要素とする配列を作成せよ

■わたし

void Start()
    {
        int[] yList = new int[31];
        yList[0] = 0;
        yList[1] = 1;
        yList[2] = 2;
        yList[3] = 3;
        yList[4] = 4;
        yList[5] = 5;
        yList[6] = 6;
        yList[7] = 7;
        yList[8] = 8;
        yList[9] = 9;
        yList[10] = 10;
        yList[11] = 11;
        yList[12] = 12;
        yList[13] = 13;
        yList[14] = 14;
        yList[15] = 15;
        yList[16] = 16;
        yList[17] = 17;
        yList[18] = 18;
        yList[19] = 19;
        yList[20] = 20;
        yList[21] = 21;
        yList[22] = 22;
        yList[23] = 23;
        yList[24] = 24;
        yList[25] = 25;
        yList[26] = 26;
        yList[27] = 27;
        yList[28] = 28;
        yList[29] = 29;
        yList[30] = 30;
        for(int i=0; i< yList.Length; i++)
        {
            Debug.Log(yList[i]);
        }
    }

■先生

        // 0から30までの数を要素とする配列を作成せよ
        int[] yList = new int[31];
        for(int i=0; i<yList.Length; i++)
        {
            yList[i] = i;
        }
        for(int i=0; i<yList.Length; i++)
        {
            Debug.Log(yList[i]);
        }

■まとめ
①壮大に間違えている様ですが、
 よくみると「わたし」も2個目のDebug.Logは正解ですね。間違えてますけどね。

②forを2回使うなんて、
 1回目のforで配列の要素に1足す、2回目のforでDebug.Logに1個表示、、、
 をアップデート関数でやってる。

③あとは1回目のforが分れば、理解できそう。。

for(int i=0; i<yList.Length; i++)
最初は0スタート、yListの長さより長くなるまでやる、1ずつ増やす

ここまでは割とわかるかも。つまづいたのがこの先

yList[i] = i;

むむむ、何を更新しているのか。

そこでばらして考えると、、、

yList[i] = i;


yList[i]  =  i
👆要素の数 👆for文で宣言されたi


yListの要素の数にはfor文で宣言されたiを代入する
ってなると思うよ。

学習教材

[Unityゲーム開発入門:インディーゲームクリエイターが教えるマリオのような2Dゲームを作成する方法【スタジオしまづ】](https://www.udemy.com/course/studio_shimazu_sideview_action/)

1
0
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?