← ゼロから始める C# レッスン 21 / 39 · 10 XP

配列

解説と問題文

機械翻訳です。ネイティブスピーカーによる確認はまだです。

配列は 同じ型 の要素を長さを決めて集めたものです:

int[] scores = { 10, 25, 7 };
string[] names = new string[3];     // null が 3 つ
scores[0] = 15;                     // インデックスは 0 から
Console.WriteLine(scores.Length);   // 3
foreach (int s in scores) Console.WriteLine(s);

範囲の外に出ると IndexOutOfRangeException になります(C のように黙ってメモリを壊す代わりに)。

System の便利なもの:

Array.Sort(scores);                 // 昇順、その場で
Array.Reverse(scores);
int max = scores.Max();             // using System.Linq; が必要です
int[,] grid = new int[3, 4];        // 2 次元: grid[y, x]

長さは変えられません — そのためには List<T> があります(using System.Collections.Generic;)。

課題. 1 行目は n、2 行目は空白で区切られた整数 n 個です。それらを昇順に空白で区切って出力し、 次の行に平均を小数点以下 1 桁で出力してください。

無料で登録すると、この課題をブラウザーでそのまま解けます: コードエディター、実行、テストでのチェック、ヒント。