C# 에서의 2차원 배열 사용에 관련된 예제http://www.devpia.com/maeul/contents/de ··· %3D23556
-- 소스 --
using System;
namespace TestArray
{
public class Dimensions
{
public Dimensions()
{
}
public string[][] GetData(int first, int second)
{
string[][] str = new string[first][];
for (int i=0; i<first; i++)
{
str[i] = new string[second];
for (int j=0; j<second; j++)
{
str[i][j] = String.Format("str[{0}][{1}]=\"{0}, {1}\"", i, j, i, j);
}
}
return str;
}
public static void Main()
{
Dimensions dim = new Dimensions();
string[][] dims = dim.GetData(3, 4);
// for
Console.WriteLine("-- For statement");
for (int i=0; i<dims.Length; i++)
{
for (int j=0; j<dims[i].Length; j++)
{
Console.Write(dims[i][j] + " ");
}
Console.Write("\n");
}
Console.WriteLine("");
// foreach
Console.WriteLine("-- Foreach statement");
foreach (string[] s1 in dims)
{
foreach (string s2 in s1)
{
Console.Write(s2 + " ");
}
Console.Write("\n");
}
}
}
}
-- 결과 --
C:\VS.NET\cmd>csc Dimensions.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (c) Microsoft Corp 2000-2001. All rights reserved.
C:\VS.NET\cmd>Dimensions
-- For statement
str[0][0]="0, 0" str[0][1]="0, 1" str[0][2]="0, 2" str[0][3]="0, 3"
str[1][0]="1, 0" str[1][1]="1, 1" str[1][2]="1, 2" str[1][3]="1, 3"
str[2][0]="2, 0" str[2][1]="2, 1" str[2][2]="2, 2" str[2][3]="2, 3"
-- Foreach statement
str[0][0]="0, 0" str[0][1]="0, 1" str[0][2]="0, 2" str[0][3]="0, 3"
str[1][0]="1, 0" str[1][1]="1, 1" str[1][2]="1, 2" str[1][3]="1, 3"
str[2][0]="2, 0" str[2][1]="2, 1" str[2][2]="2, 2" str[2][3]="2, 3"



댓글을 달아 주세요