配列
1次配列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication25
{
class Program
{
static void Main(string[] args)
{
int[] list;
list = new int[10];
list[0] = 10;
list[1] = 20;
list[2] = 30;
list[3] = 40;
list[4] = 50;
list[5] = 60;
list[6] = 70;
list[7] = 80;
list[8] = 90;
list[9] = 100;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Sample int list = " + list[i]);
}
char[] s;
s = new char[10];
s[0] = 'a';
s[1] = 'b';
s[2] = 'c';
s[3] = 'd';
s[4] = 'e';
s[5] = 'f';
s[6] = 'g';
s[7] = 'h';
s[8] = '\n';
s[9] = '\0';
string str = new string(s);
Console.WriteLine(str);
Console.ReadKey();
}
}
}
多次元配列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication26
{
class Program
{
static void Main(string[] args)
{
int[,] list;
list = new int[2, 2];
list[0, 0] = 10;
list[0, 1] = 20;
list[1, 0] = 30;
list[1, 1] = 40;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("i = {0} ,j= {1}, list={2}", i, j, list[i, j]);
}
}
Console.ReadKey();
}
}
}
ジャグ配列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication27
{
class Program
{
static void Main(string[] args)
{
int[][] list;
// ジャグ配列の場合
list = new int[2][];
// 配列に配列を設定
list[0] = new int[3]{10, 20, 30};
list[1] = new int[2]{40, 50};
for (int i = 0; i < 2; i++)
{
// 可変する場合はforeachでまわす
foreach (int a in list[i])
{
Console.Write("list = {0} ", a);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
配列が多次元配列と異なり、可変になる場合はこの方法を使用する
最終更新:2011年03月06日 20:40