はじめに
2次元配列は、C#プログラミングにおいて表形式のデータを扱う際の基本的なデータ構造です。ゲーム開発での迷路マップの表現や、画像処理、行列計算など、様々な場面で活躍します。本記事では、2次元配列の基礎から実践的な使い方、そしてパフォーマンス最適化まで、現場で必要な知識を体系的に解説します。これから2次元配列を学ぶ方はもちろん、既に使用している方にとっても新しい発見があるはずです。
1. 2次元配列の基本概念と特徴
2. 基本的な使い方とテクニック
3. 実践的な活用例
4. パフォーマンス最適化の方法
5. 一般的なバグとその対処法
6. 高度な操作テクニック
7. 代替データ構造との比較と使い分け
2次元配列とは?初心者にもわかりやすく解説
メモリ上に行と列で並ぶデータ構造の特徴
2次元配列は、データを行(row)と列(column)の格子状に配置できるデータ構造です。表計算ソフトのような縦横のグリッド構造をプログラム上で表現する際に非常に便利です。
1. メモリ配置
- 連続したメモリ領域に格納される
- 行優先順序(Row-major order)でメモリに配置
- 各要素に高速アクセスが可能
2. アクセス方法
- [行,列]の形式で要素にアクセス
- 0から始まるインデックスを使用
// 3行4列の2次元配列の宣言と初期化
int[,] array2D = new int[3, 4] {
{1, 2, 3, 4}, // 0行目
{5, 6, 7, 8}, // 1行目
{9, 10, 11, 12} // 2行目
};
// 1行目、2列目の要素(値:6)にアクセス
int element = array2D[1, 2]; // 値7が取得できる
1次元配列との違いと利点
1. 構造の違い
| 特徴 | 1次元配列 | 2次元配列 |
|---|---|---|
| データの配置 | 一列に並ぶ | 格子状に並ぶ |
| インデックス | 1つ | 2つ(行と列) |
| メモリ効率 | 高い | やや低い |
| 用途 | リスト型データ | 表形式データ |
2. 2次元配列の利点
- 直感的なデータ構造(表形式で考えやすい)
- 行と列の関係性を維持しやすい
- マトリックス演算に適している
- ゲームの盤面やピクセル処理に最適
// 1次元配列で9マスの盤面を表現
int[] board1D = new int[9] {1, 0, 1, 0, 1, 0, 1, 0, 1};
// 2次元配列で同じ盤面をより直感的に表現
int[,] board2D = new int[3, 3] {
{1, 0, 1},
{0, 1, 0},
{1, 0, 1}
};
このように、2次元配列は表形式のデータを扱う際に非常に便利なデータ構造です。特に、ゲーム開発や画像処理、科学技術計算など、多くの実践的な場面で活用されています。次のセクションでは、この2次元配列の基本的な使い方について詳しく見ていきましょう。
2次元配列の基本的な使い方をマスターしよう
配列の宣言と初期化の正しい方法
2次元配列の宣言と初期化には複数の方法があります。それぞれの特徴を理解して、適切な方法を選択しましょう。
1. 宣言と同時に初期化する方法
// 方法1: 明示的な初期化
int[,] array1 = new int[2, 3] {
{1, 2, 3},
{4, 5, 6}
};
// 方法2: サイズを省略した初期化
int[,] array2 = {
{1, 2, 3},
{4, 5, 6}
};
2. 宣言後に値を設定する方法
// まず配列を宣言
int[,] array3 = new int[2, 3];
// 後から値を設定
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
array3[i, j] = i * 3 + j + 1;
}
}
要素へのアクセスと値の代入テクニック
2次元配列の要素へのアクセスには、行と列のインデックスを使用します。以下に主要なテクニックを示します。
int[,] matrix = new int[3, 4];
// 基本的なアクセスと代入
matrix[0, 0] = 1; // 左上の要素
matrix[2, 3] = 12; // 右下の要素
// 行単位でのアクセス
for (int j = 0; j < matrix.GetLength(1); j++) {
matrix[1, j] = j * 2; // 1行目の各要素に値を設定
}
// 列単位でのアクセス
for (int i = 0; i < matrix.GetLength(0); i++) {
matrix[i, 2] = i * 3; // 2列目の各要素に値を設定
}
配列のサイズと範囲の確認方法
2次元配列のサイズと範囲を適切に管理することは、安全なプログラミングの基本です。
int[,] array = new int[4, 3];
// サイズの取得
int rows = array.GetLength(0); // 行数を取得
int columns = array.GetLength(1); // 列数を取得
int totalElements = array.Length; // 全要素数を取得
// 安全なアクセスのための範囲チェック
public static bool IsValidIndex(int[,] array, int row, int col) {
return row >= 0 && row < array.GetLength(0) &&
col >= 0 && col < array.GetLength(1);
}
// 使用例
if (IsValidIndex(array, 2, 1)) {
array[2, 1] = 10; // 安全なアクセス
}
便利なユーティリティメソッド
拡張メソッドを作成することにより、便利なユーティリティメソッドを作成することができます。
public static class Array2DExtensions {
// 2次元配列の内容を文字列として表示
public static string ToDisplayString(this int[,] array) {
var result = new System.Text.StringBuilder();
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
result.Append(array[i, j].ToString().PadLeft(4));
}
result.AppendLine();
}
return result.ToString();
}
}
// 使用例
int[,] matrix = new int[,] {{1, 2, 3}, {4, 5, 6}};
Console.WriteLine(matrix.ToDisplayString());
これらの基本的な操作を確実に習得することで、より複雑な2次元配列の操作も簡単に行えるようになります。次のセクションでは、これらの基本操作を活用した実践的な使用例を見ていきましょう。
実践的な2次元配列の活用シーン
ゲーム開発での迷路やマップの表現
ゲーム開発において、2次元配列は地形やマップの表現に最適です。以下に実践的な例を示します。
public class GameMap {
private int[,] map;
// マップの種類を表す定数
public const int WALL = 1;
public const int PATH = 0;
public const int GOAL = 2;
public GameMap(int width, int height) {
map = new int[height, width];
}
// シンプルな迷路の生成例
public void GenerateSimpleMaze() {
// 外壁の設置
for (int i = 0; i < map.GetLength(0); i++) {
for (int j = 0; j < map.GetLength(1); j++) {
if (i == 0 || i == map.GetLength(0) - 1 ||
j == 0 || j == map.GetLength(1) - 1) {
map[i, j] = WALL;
}
}
}
// ゴールの設置
map[map.GetLength(0) - 2, map.GetLength(1) - 2] = GOAL;
}
// マップの表示
public void DisplayMap() {
for (int i = 0; i < map.GetLength(0); i++) {
for (int j = 0; j < map.GetLength(1); j++) {
switch (map[i, j]) {
case WALL: Console.Write("█"); break;
case PATH: Console.Write(" "); break;
case GOAL: Console.Write("G"); break;
}
}
Console.WriteLine();
}
}
}
行列計算での利用方法
数値計算や科学技術計算で頻繁に使用される行列計算の実装例です。
public static class MatrixOperations {
// 行列の乗算
public static double[,] Multiply(double[,] matrix1, double[,] matrix2) {
int rows1 = matrix1.GetLength(0);
int cols1 = matrix1.GetLength(1);
int cols2 = matrix2.GetLength(1);
if (cols1 != matrix2.GetLength(0)) {
throw new ArgumentException("行列の次元が不適切です");
}
double[,] result = new double[rows1, cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i, j] += matrix1[i, k] * matrix2[k, j];
}
}
}
return result;
}
// 行列の加算
public static double[,] Add(double[,] matrix1, double[,] matrix2) {
int rows = matrix1.GetLength(0);
int cols = matrix1.GetLength(1);
if (rows != matrix2.GetLength(0) || cols != matrix2.GetLength(1)) {
throw new ArgumentException("行列のサイズが一致しません");
}
double[,] result = new double[rows, cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i, j] = matrix1[i, j] + matrix2[i, j];
}
}
return result;
}
}
画像処理における2次元配列の使い方
画像処理では、ピクセルデータの操作に2次元配列が活用されます。以下は簡単なグレースケール処理の例です。
public class ImageProcessor {
// グレースケール画像を表現する2次元配列
private byte[,] pixels;
public ImageProcessor(int width, int height) {
pixels = new byte[height, width];
}
// 3x3の平均化フィルタを適用する例
public void ApplyAverageFilter() {
byte[,] result = new byte[pixels.GetLength(0), pixels.GetLength(1)];
for (int i = 1; i < pixels.GetLength(0) - 1; i++) {
for (int j = 1; j < pixels.GetLength(1) - 1; j++) {
int sum = 0;
// 3x3の範囲で平均を計算
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
sum += pixels[i + di, j + dj];
}
}
result[i, j] = (byte)(sum / 9);
}
}
pixels = result;
}
// エッジ検出の簡単な実装例
public void DetectEdges() {
byte[,] result = new byte[pixels.GetLength(0), pixels.GetLength(1)];
for (int i = 1; i < pixels.GetLength(0) - 1; i++) {
for (int j = 1; j < pixels.GetLength(1) - 1; j++) {
// 簡単な縦方向のエッジ検出
int edgeValue = Math.Abs(
pixels[i + 1, j] - pixels[i - 1, j]
);
result[i, j] = (byte)Math.Min(255, edgeValue);
}
}
pixels = result;
}
}
これらの実装例は、2次元配列の実践的な活用方法を示しています。次のセクションでは、これらの処理をより効率的に行うためのパフォーマンス最適化テクニックについて説明します。
パフォーマンスを意識した2次元配列の操作
メモリ効率を考慮したループ処理の実装
2次元配列のパフォーマンスを最適化する上で、メモリアクセスパターンの理解が重要です。以下に主要な最適化テクニックを示します。
public class ArrayPerformance {
// GetLength()の呼び出しを最小化
public static void OptimizedLoop(int[,] array) {
// ループの外でサイズを取得
int rows = array.GetLength(0);
int cols = array.GetLength(1);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i, j] = i + j;
}
}
}
// 非効率なアクセスパターン
public static void InefficientAccess(int[,] array) {
int rows = array.GetLength(0);
int cols = array.GetLength(1);
// 列優先でのアクセス - キャッシュミスが多発
for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
array[i, j] = i + j;
}
}
}
// 効率的なアクセスパターン
public static void EfficientAccess(int[,] array) {
int rows = array.GetLength(0);
int cols = array.GetLength(1);
// 行優先でのアクセス - メモリの局所性を活かせる
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i, j] = i + j;
}
}
}
}
大規模データ処理時の最適化テクニック
大規模なデータを扱う場合の最適化テクニックを紹介します。
public class LargeArrayOptimization {
// パラレル処理を活用した最適化
public static void ParallelProcessing(int[,] array) {
int rows = array.GetLength(0);
int cols = array.GetLength(1);
// 行単位での並列処理
Parallel.For(0, rows, i => {
for (int j = 0; j < cols; j++) {
array[i, j] = ProcessElement(i, j);
}
});
}
// メモリ使用量を最適化するためのチャンク処理
public static void ProcessInChunks(int[,] largeArray, int chunkSize) {
int rows = largeArray.GetLength(0);
int cols = largeArray.GetLength(1);
// チャンク単位で処理
for (int i = 0; i < rows; i += chunkSize) {
int endRow = Math.Min(i + chunkSize, rows);
ProcessChunk(largeArray, i, endRow);
}
}
private static void ProcessChunk(int[,] array, int startRow, int endRow) {
int cols = array.GetLength(1);
for (int i = startRow; i < endRow; i++) {
for (int j = 0; j < cols; j++) {
array[i, j] = ProcessElement(i, j);
}
}
}
private static int ProcessElement(int i, int j) {
// 実際の処理をここに実装
return i * j;
}
// SIMD操作の活用例(.NET Core 3.0以降)
public static void SimdOptimization(int[,] array) {
int rows = array.GetLength(0);
int cols = array.GetLength(1);
// 1次元配列としてSIMD処理
Span<int> span = new Span<int>(array);
for (int i = 0; i < span.Length; i += 4) {
if (i + 4 <= span.Length) {
// 4要素同時に処理
span[i] *= 2;
span[i + 1] *= 2;
span[i + 2] *= 2;
span[i + 3] *= 2;
}
}
}
}
パフォーマンスを意識した2次元配列の操作のまとめ
これらの最適化テクニックを適切に組み合わせることで、2次元配列を使用した処理のパフォーマンスを大幅に向上させることができます。次のセクションでは、実装時に注意すべきバグと対処法について説明します。
- メモリアクセスパターン
- 行優先でのアクセスを基本とする
- 連続したメモリアクセスを心がける
- キャッシュミスを最小限に抑える
- 計算の最適化
- ループ内での計算を最小限に
- 共通の計算は事前に実行
- 配列境界チェックを最適化
- リソース管理
- 大きな配列は適切に解放
- メモリ断片化を防ぐ
- 必要に応じてチャンク処理を導入
2次元配列での一般的なバグと対処法
範囲外アクセスを防ぐ安全な実装方法
2次元配列で最も多く発生するバグは、配列の範囲外アクセスです。これを防ぐための実装方法を示します。
public class SafeArrayAccess {
private readonly int[,] array;
public SafeArrayAccess(int rows, int columns) {
array = new int[rows, columns];
}
// 安全なアクセサメソッド
public bool TryGetValue(int row, int col, out int value) {
value = default;
if (!IsValidIndex(row, col)) {
return false;
}
value = array[row, col];
return true;
}
// 範囲チェック付きの値設定
public bool TrySetValue(int row, int col, int value) {
if (!IsValidIndex(row, col)) {
return false;
}
array[row, col] = value;
return true;
}
// インデックスの妥当性チェック
private bool IsValidIndex(int row, int col) {
return row >= 0 && row < array.GetLength(0) &&
col >= 0 && col < array.GetLength(1);
}
// 境界チェック付きの範囲操作
public void ProcessRegion(int startRow, int startCol, int endRow, int endCol, Action<int, int> action) {
// 境界の正規化
startRow = Math.Max(0, startRow);
startCol = Math.Max(0, startCol);
endRow = Math.Min(array.GetLength(0), endRow);
endCol = Math.Min(array.GetLength(1), endCol);
for (int i = startRow; i < endRow; i++) {
for (int j = startCol; j < endCol; j++) {
action(i, j);
}
}
}
}
メモリリークを防ぐベストプラクティス
2次元配列を使用する際のメモリ管理のベストプラクティスを示します。
public class ArrayMemoryManagement {
// 大きな配列を扱うためのクラス
public class LargeArrayHandler : IDisposable {
private int[,] largeArray;
private bool disposed = false;
public LargeArrayHandler(int rows, int cols) {
largeArray = new int[rows, cols];
}
// 配列の一部をクリア
public void ClearRegion(int startRow, int endRow) {
if (disposed) throw new ObjectDisposedException(nameof(LargeArrayHandler));
for (int i = startRow; i < endRow; i++) {
Array.Clear(largeArray, i * largeArray.GetLength(1), largeArray.GetLength(1));
}
}
// 適切なリソース解放
public void Dispose() {
if (!disposed) {
largeArray = null;
disposed = true;
GC.SuppressFinalize(this);
}
}
// 配列のコピーを安全に作成
public int[,] CreateSafeCopy() {
if (disposed) throw new ObjectDisposedException(nameof(LargeArrayHandler));
int rows = largeArray.GetLength(0);
int cols = largeArray.GetLength(1);
int[,] copy = new int[rows, cols];
Array.Copy(largeArray, copy, largeArray.Length);
return copy;
}
}
// 一時配列の適切な使用例
public static void ProcessWithTempArray(int[,] source) {
try {
// 一時配列の作成
int[,] temp = new int[source.GetLength(0), source.GetLength(1)];
// 処理の実行
ProcessArray(source, temp);
// 結果をソースにコピー
Array.Copy(temp, source, temp.Length);
}
catch (OutOfMemoryException) {
// メモリ不足時の処理
Console.WriteLine("メモリが不足しています。より小さな範囲で処理を行ってください。");
}
}
private static void ProcessArray(int[,] source, int[,] temp) {
// 実際の処理をここに実装
}
}
一般的なバグとその対処法のまとめ
これらの対策を適切に実装することで、2次元配列を使用したプログラムの信頼性を大きく向上させることができます。次のセクションでは、より高度な操作テクニックについて説明します。
1. 範囲外アクセス
- 必ず境界チェックを実装
- TryPatternの活用
- 安全なアクセサメソッドの提供
2. メモリ関連の問題
- IDisposableパターンの適切な実装
- 大きな配列の適切な解放
- 一時配列の慎重な使用
3. 並行処理の問題
- スレッドセーフな実装の検討
- 適切な同期機構の使用
- 競合状態の回避
より高度な2次元配列の操作テクニック
LINQを使った効率的な配列操作
2次元配列でもLINQを活用することで、より簡潔で表現力豊かなコードを書くことができます。
public static class Array2DLinqExtensions {
// 2次元配列をLINQで扱いやすくする拡張メソッド
public static IEnumerable<T> AsEnumerable<T>(this T[,] array) {
for (int i = 0; i < array.GetLength(0); i++) {
for (int j = 0; j < array.GetLength(1); j++) {
yield return array[i, j];
}
}
}
// 行を取得する拡張メソッド
public static IEnumerable<T> GetRow<T>(this T[,] array, int row) {
for (int j = 0; j < array.GetLength(1); j++) {
yield return array[row, j];
}
}
// 列を取得する拡張メソッド
public static IEnumerable<T> GetColumn<T>(this T[,] array, int column) {
for (int i = 0; i < array.GetLength(0); i++) {
yield return array[i, column];
}
}
// LINQ操作の実践例
public static class LinqExamples {
public static void DemonstrateLINQOperations() {
int[,] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 全要素の平均を計算
double average = matrix.AsEnumerable().Average();
// 5より大きい要素を検索
var largeNumbers = matrix.AsEnumerable()
.Where(x => x > 5)
.ToList();
// 各行の合計を計算
var rowSums = Enumerable.Range(0, matrix.GetLength(0))
.Select(row => matrix.GetRow(row).Sum())
.ToList();
// 対角要素の取得
var diagonal = Enumerable.Range(0, Math.Min(matrix.GetLength(0), matrix.GetLength(1)))
.Select(i => matrix[i, i])
.ToList();
}
}
}
ジャグ配列との使い分けポイント
2次元配列とジャグ配列の特性を理解し、適切に使い分けることが重要です。
public class ArrayComparisonExample {
// 2次元配列とジャグ配列の性能比較
public static void ComparePerformance() {
const int size = 1000;
// 2次元配列
int[,] regular2DArray = new int[size, size];
// ジャグ配列
int[][] jaggedArray = new int[size][];
for (int i = 0; i < size; i++) {
jaggedArray[i] = new int[size];
}
// 2次元配列のアクセス
var regularArrayWatch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
regular2DArray[i, j] = i + j;
}
}
regularArrayWatch.Stop();
// ジャグ配列のアクセス
var jaggedArrayWatch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < size; i++) {
for (int j = 0; j < jaggedArray[i].Length; j++) {
jaggedArray[i][j] = i + j;
}
}
jaggedArrayWatch.Stop();
}
// 可変長行の実装例
public class VariableLengthRowExample {
private int[][] dataRows;
public VariableLengthRowExample(int[] rowLengths) {
dataRows = new int[rowLengths.Length][];
for (int i = 0; i < rowLengths.Length; i++) {
dataRows[i] = new int[rowLengths[i]];
}
}
// 行ごとの操作
public void ProcessRow(int rowIndex, Action<int[]> processor) {
if (rowIndex >= 0 && rowIndex < dataRows.Length) {
processor(dataRows[rowIndex]);
}
}
}
// 使用例と比較
public static void DemonstrateArrayTypes() {
// 2次元配列 - 均一なグリッドに適している
int[,] chessboard = new int[8, 8];
// ジャグ配列 - 不規則なデータ構造に適している
int[][] triangle = new int[5][];
for (int i = 0; i < 5; i++) {
triangle[i] = new int[i + 1];
}
}
}
高度な2次元配列の操作テクニックのまとめ
これらの高度なテクニックを適切に使用することで、より効率的で保守性の高いコードを書くことができます。次のセクションでは、2次元配列の代替手段について詳しく見ていきましょう。
1. 2次元配列を選ぶ場合
- データが常に矩形(行列)の場合
- メモリ効率が重要な場合
- シンプルな実装が望ましい場合
2. ジャグ配列を選ぶ場合
- 行ごとに長さが異なる場合
- 動的に行を追加/削除する必要がある場合
- 行単位の操作が多い場合
3. パフォーマンスの考慮点
- メモリアクセスパターン
- キャッシュの効率的な利用
- 実行時のオーバーヘッド
2次元配列の代替手段と使い分け
Listやジャグ配列との比較
データ構造の選択は、アプリケーションの要件によって大きく変わります。それぞれの特徴を比較して見ていきましょう。
public class DataStructureComparison {
// 各データ構造の特徴を示す実装例
public static class Examples {
// 2次元配列による実装
public static void Using2DArray() {
int[,] grid = new int[3, 4];
// メリット:シンプルなアクセス
grid[1, 2] = 42;
// サイズの取得
int rows = grid.GetLength(0); // 3
int cols = grid.GetLength(1); // 4
}
// List<List<T>>による実装
public static void UsingNestedLists() {
var grid = new List<List<int>>();
// 動的な行の追加が可能
for (int i = 0; i < 3; i++) {
grid.Add(new List<int> { 1, 2, 3, 4 });
}
// 要素の追加や削除が容易
grid[1].Add(5); // 行の末尾に追加
grid.Add(new List<int>()); // 新しい行を追加
}
// ジャグ配列による実装
public static void UsingJaggedArray() {
int[][] jaggedArray = new int[3][];
// 各行を個別に初期化可能
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[3]; // 異なる長さも可能
jaggedArray[2] = new int[5];
}
}
// パフォーマンス比較のためのベンチマーク
public static void PerformanceComparison() {
const int size = 1000;
// 2次元配列
int[,] array2D = new int[size, size];
// List<List<T>>
var nestedLists = new List<List<int>>();
for (int i = 0; i < size; i++) {
nestedLists.Add(new List<int>(size));
for (int j = 0; j < size; j++) {
nestedLists[i].Add(0);
}
}
// ジャグ配列
int[][] jagged = new int[size][];
for (int i = 0; i < size; i++) {
jagged[i] = new int[size];
}
// アクセス速度の比較
void CompareAccess() {
// 2次元配列のアクセス
array2D[500, 500] = 1;
// ネストされたListのアクセス
nestedLists[500][500] = 1;
// ジャグ配列のアクセス
jagged[500][500] = 1;
}
}
}
用途に応じた最適なデータ構造の選択方法
以下の表は、各データ構造の特徴と適した用途をまとめたものです:
| データ構造 | メモリ効率 | アクセス速度 | 柔軟性 | 主な用途 |
|---|---|---|---|---|
| 2次元配列 | 最高 | 高速 | 低い | 固定サイズの行列、画像処理 |
| List<List<T>> | 中 | 中程度 | 最高 | 動的なデータ、頻繁な要素の追加/削除 |
| ジャグ配列 | 高 | 高速 | 中程度 | 可変長の行を持つデータ |
以下に選択の指針を示します。
1. 2次元配列を選ぶ場合
public static class Array2DUseCases {
// 画像処理
public static void ImageProcessing() {
byte[,] pixels = new byte[1024, 768];
// 高速な要素アクセスが可能
}
// 行列演算
public static void MatrixOperations() {
double[,] matrix = new double[100, 100];
// メモリ効率が良く、行列計算に適している
}
}
2. List<List<T>>を選ぶ場合
public static class NestedListUseCases {
// 動的なグリッドデータ
public static void DynamicGrid() {
var grid = new List<List<string>>();
// 行の追加が容易
grid.Add(new List<string> { "A", "B", "C" });
// 列の追加も簡単
foreach (var row in grid) {
row.Add("New Column");
}
}
// スパースマトリックス(疎行列)
public static void SparseMatrix() {
var sparseMatrix = new List<List<(int col, double value)>>();
// 値が存在する要素のみを保存
}
}
3. ジャグ配列を選ぶ場合
public static class JaggedArrayUseCases {
// 三角形状のデータ
public static void TriangularData() {
int[][] triangle = new int[5][];
for (int i = 0; i < 5; i++) {
triangle[i] = new int[i + 1];
}
}
// 行ごとに異なるサイズが必要な場合
public static void VariableSizeRows() {
string[][] text = new string[3][];
text[0] = new string[] { "短い行" };
text[1] = new string[] { "やや", "長い", "行" };
text[2] = new string[] { "最も", "長い", "行", "です" };
}
}
これらの代替手段を理解し、適切に使い分けることで、より効率的で保守性の高いコードを書くことができます。重要なのは、アプリケーションの要件(パフォーマンス、メモリ使用量、保守性など)を慎重に検討し、最適なデータ構造を選択することです。
2次元配列のまとめ
この記事では、C#における2次元配列の全体像を解説してきました。基本的な使い方から、実践的な活用例、パフォーマンス最適化、さらには代替手段との比較まで、幅広く取り上げました。2次元配列は単純なデータ構造でありながら、適切に使用することで強力な開発ツールとなります。用途に応じて最適な使い方を選択し、パフォーマンスと保守性のバランスを取りながら実装することで、より良いプログラムを作ることができるでしょう。今回学んだ知識を活かして、ぜひ実践で活用してみてください。

