using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Array myArr1 = Array.CreateInstance(typeof(int),10);
Console.Write($"myArr1.GetLowerBound(0) : {myArr1.GetLowerBound(0)} "); //0
Console.Write($"myArr1.GetUpperBound(0) : {myArr1.GetUpperBound(0)} "); //9
Console.WriteLine();
for (int i = myArr1.GetLowerBound(0); i <= myArr1.GetUpperBound(0); i++) //<=๋ก ์จ์ผํจ!
{
myArr1.SetValue(i*10,i);
Console.Write($" {myArr1.GetValue(i)} ");
}
}
}
}
์ถ๋ ฅ
0 10 20 30 40 50 60 70 80 90
Array.CreateInstance ๋ฉ์๋ (System)
Array ํด๋์ค์ ์ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
learn.microsoft.com
์ ์
Array ํด๋์ค์ ์ ์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
์ค๋ฒ๋ก๋
CreateInstance(Type, Int32) | ์ง์ ํ Type ๋ฐ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ 1์ฐจ์ Array๋ฅผ ๋ง๋ญ๋๋ค. |
CreateInstance(Type, Int32[]) | ์ง์ ํ Type ๋ฐ ์ฐจ์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ ๋ค์ฐจ์ Array๋ฅผ ๋ง๋ญ๋๋ค. ์ฐจ์ ๊ธธ์ด๊ฐ 32๋นํธ ์ ์ ๋ฐฐ์ด๋ก ์ง์ ๋์ด ์์ต๋๋ค. |
CreateInstance(Type, Int64[]) | ์ง์ ํ Type ๋ฐ ์ฐจ์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ ๋ค์ฐจ์ Array๋ฅผ ๋ง๋ญ๋๋ค. ์ฐจ์ ๊ธธ์ด๊ฐ 64๋นํธ ์ ์ ๋ฐฐ์ด๋ก ์ง์ ๋์ด ์์ต๋๋ค. |
CreateInstance(Type, Int32, Int32) | 0๋ถํฐ ์์ํ๋ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ ๋ Type ๋ฐ ์ฐจ์ ๊ธธ์ด์ 2์ฐจ์ Array๋ฅผ ๋ง๋ญ๋๋ค. |
CreateInstance(Type, Int32[], Int32[]) | ์ง์ ํ ํํ์ ์ฌ์ฉํ์ฌ ์ง์ ํ Array ๋ฐ ์ฐจ์ ๊ธธ์ด์ ๋ค์ฐจ์ Type ์ ๋ง๋ญ๋๋ค. |
CreateInstance(Type, Int32, Int32, Int32) | ์ง์ ํ Type ๋ฐ ์ฐจ์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ ์ผ์ฐจ์ Array๋ฅผ ๋ง๋ญ๋๋ค. |
CreateInstance(Type, Int32)
public static Array CreateInstance (Type elementType, int length);
์์
๋ค์ ์ฝ๋ ์์ ์์๋ 1์ฐจ์ Array์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
using System;
public class SamplesArray {
public static void Main() {
// Creates and initializes a one-dimensional Array of type int.
Array my1DArray=Array.CreateInstance( typeof(int), 5 );
for ( int i = my1DArray.GetLowerBound(0); i <= my1DArray.GetUpperBound(0); i++ )
my1DArray.SetValue( i+1, i );
// Displays the values of the Array.
Console.WriteLine( "The one-dimensional Array contains the following values:" );
PrintValues( my1DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The one-dimensional Array contains the following values:
1 2 3 4 5
*/
CreateInstance(Type, Int32[])
public static Array CreateInstance (Type elementType, params int[] lengths);
์์
๋ค์ ์ฝ๋ ์์ ์์๋ ๋ค์ฐจ์์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค Array.
using System;
public class SamplesArray3 {
public static void Main() {
// Creates and initializes a multidimensional Array of type string.
int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
Array my4DArray=Array.CreateInstance( typeof(string), myLengthsArray );
for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ ) {
int[] myIndicesArray = new int[4] { i, j, k, l };
my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
}
// Displays the values of the Array.
Console.WriteLine( "The four-dimensional Array contains the following values:" );
PrintValues( my4DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The four-dimensional Array contains the following values:
0000 0001 0002 0003 0004
0010 0011 0012 0013 0014
0020 0021 0022 0023 0024
0030 0031 0032 0033 0034
0100 0101 0102 0103 0104
0110 0111 0112 0113 0114
0120 0121 0122 0123 0124
0130 0131 0132 0133 0134
0200 0201 0202 0203 0204
0210 0211 0212 0213 0214
0220 0221 0222 0223 0224
0230 0231 0232 0233 0234
1000 1001 1002 1003 1004
1010 1011 1012 1013 1014
1020 1021 1022 1023 1024
1030 1031 1032 1033 1034
1100 1101 1102 1103 1104
1110 1111 1112 1113 1114
1120 1121 1122 1123 1124
1130 1131 1132 1133 1134
1200 1201 1202 1203 1204
1210 1211 1212 1213 1214
1220 1221 1222 1223 1224
1230 1231 1232 1233 1234
*/
์ค๋ช
๋๋ถ๋ถ์ ํด๋์ค์ Array ๋ฌ๋ฆฌ ๊ณต์ฉ ์์ฑ์ ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ CreateInstance ๋ฐํ์์ ๋ฐ์ธ๋ฉ๋ ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
๋ฐฐ์ด์ ์์ lengths ์๋ ์ Array์ฐจ์์ ์์ ๊ฐ์์ผ ํฉ๋๋ค. ๋ฐฐ์ด์ lengths ๊ฐ ์์๋ ์ Array์ฐจ์์ ๊ธธ์ด๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
์ฐธ์กฐ ํ์ ์์๊ฐ .๋ก null์ด๊ธฐํ๋ฉ๋๋ค. ๊ฐ ํ์ ์์๋ 0์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
์ด ๋ฉ์๋๋ O(n) ์ฐ์ฐ์ ๋๋ค. ์ฌ๊ธฐ์ n ๋ชจ๋ ๊ฐ์ ๊ณฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค lengths.
์ ์ฉ ๋์
.NET 7 RC 1 ๋ฐ ๊ธฐํ ๋ฒ์ CreateInstance(Type, Int64[])
public static Array CreateInstance (Type elementType, params long[] lengths);
๋งค๊ฐ ๋ณ์
๋ง๋ค Array์ ๊ฐ ์ฐจ์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ 64๋นํธ ์ ์ ๋ฐฐ์ด์ ๋๋ค. ๋ฐฐ์ด์ ๊ฐ ์ ์๋ 0๊ณผ Int32.MaxValue(ํฌํจ) ์ฌ์ด์ฌ์ผ ํฉ๋๋ค.
๋ฐํ
์ง์ ํ Type ๋ฐ ๊ฐ ์ฐจ์์ ๋ํด ์ง์ ํ ๊ธธ์ด๋ฅผ ๊ฐ์ง ์ธ๋ฑ์ค๊ฐ 0๋ถํฐ ์์ํ๋ ์ ๋ค์ฐจ์ Array์ ๋๋ค.
์์ธ
elementType์ ์ ํจํ Type์ด ์๋๋๋ค.
๋๋
lengths ๋ฐฐ์ด์ 1๊ฐ ๋ฏธ๋ง์ ์์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
elementType์ ์ง์๋์ง ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด Void๋ ์ง์๋์ง ์์ต๋๋ค.
๋๋
elementType์ด ๊ฐ๋ฐฉํ ์ ๋ค๋ฆญ ํ์์ธ ๊ฒฝ์ฐ.
๋ชจ๋ lengths ๊ฐ์ด 0๋ณด๋ค ์๊ฑฐ๋ Int32.MaxValue๋ณด๋ค ํฝ๋๋ค.
์์
๋ค์ ์ฝ๋ ์์ ์์๋ ๋ค์ฐจ์์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค Array.
using System;
public class SamplesArray3 {
public static void Main() {
// Creates and initializes a multidimensional Array of type string.
int[] myLengthsArray = new int[4] { 2, 3, 4, 5 };
Array my4DArray=Array.CreateInstance( typeof(string), myLengthsArray );
for ( int i = my4DArray.GetLowerBound(0); i <= my4DArray.GetUpperBound(0); i++ )
for ( int j = my4DArray.GetLowerBound(1); j <= my4DArray.GetUpperBound(1); j++ )
for ( int k = my4DArray.GetLowerBound(2); k <= my4DArray.GetUpperBound(2); k++ )
for ( int l = my4DArray.GetLowerBound(3); l <= my4DArray.GetUpperBound(3); l++ ) {
int[] myIndicesArray = new int[4] { i, j, k, l };
my4DArray.SetValue( Convert.ToString(i) + j + k + l, myIndicesArray );
}
// Displays the values of the Array.
Console.WriteLine( "The four-dimensional Array contains the following values:" );
PrintValues( my4DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The four-dimensional Array contains the following values:
0000 0001 0002 0003 0004
0010 0011 0012 0013 0014
0020 0021 0022 0023 0024
0030 0031 0032 0033 0034
0100 0101 0102 0103 0104
0110 0111 0112 0113 0114
0120 0121 0122 0123 0124
0130 0131 0132 0133 0134
0200 0201 0202 0203 0204
0210 0211 0212 0213 0214
0220 0221 0222 0223 0224
0230 0231 0232 0233 0234
1000 1001 1002 1003 1004
1010 1011 1012 1013 1014
1020 1021 1022 1023 1024
1030 1031 1032 1033 1034
1100 1101 1102 1103 1104
1110 1111 1112 1113 1114
1120 1121 1122 1123 1124
1130 1131 1132 1133 1134
1200 1201 1202 1203 1204
1210 1211 1212 1213 1214
1220 1221 1222 1223 1224
1230 1231 1232 1233 1234
*/
์ค๋ช
๋๋ถ๋ถ์ ํด๋์ค์ Array ๋ฌ๋ฆฌ ๊ณต์ฉ ์์ฑ์ ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ CreateInstance ๋ฐํ์์ ๋ฐ์ธ๋ฉ๋ ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
๋ฐฐ์ด์ ์์ lengths ์๋ ์ Array์ฐจ์์ ์์ ๊ฐ์์ผ ํฉ๋๋ค. ๋ฐฐ์ด์ lengths ๊ฐ ์์๋ ์ Array์ฐจ์์ ๊ธธ์ด๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
์ฐธ์กฐ ํ์ ์์๊ฐ .๋ก null์ด๊ธฐํ๋ฉ๋๋ค. ๊ฐ ํ์ ์์๋ 0์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
์ด ๋ฉ์๋๋ O(n) ์ฐ์ฐ์ ๋๋ค. ์ฌ๊ธฐ์ n ๋ชจ๋ ๊ฐ์ ๊ณฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค lengths.
์ ์ฉ ๋์
.NET 7 RC 1 ๋ฐ ๊ธฐํ ๋ฒ์ CreateInstance(Type, Int32, Int32)
public static Array CreateInstance (Type elementType, int length1, int length2);
๋งค๊ฐ ๋ณ์
๋ฐํ
0๋ถํฐ ์์ํ๋ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ฉฐ ๊ฐ ์ฐจ์์ด ์ง์ ๋ ๊ธธ์ด๋ก ๋ ์ง์ ๋ Type์ ์๋ก์ด 2์ฐจ์ Array์ ๋๋ค.
์์ธ
elementType์ด(๊ฐ) null์ธ ๊ฒฝ์ฐ
elementType์ ์ ํจํ Type์ด ์๋๋๋ค.
elementType์ ์ง์๋์ง ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด Void๋ ์ง์๋์ง ์์ต๋๋ค.
๋๋
elementType์ด ๊ฐ๋ฐฉํ ์ ๋ค๋ฆญ ํ์์ธ ๊ฒฝ์ฐ.
length1๊ฐ 0๋ณด๋ค ์์ ๊ฒฝ์ฐ
๋๋
length2๊ฐ 0๋ณด๋ค ์์ ๊ฒฝ์ฐ
์์
๋ค์ ์ฝ๋ ์์ ์์๋ 2์ฐจ์ Array์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
using System;
public class SamplesArray1 {
public static void Main() {
// Creates and initializes a two-dimensional Array of type string.
Array my2DArray=Array.CreateInstance( typeof(string), 2, 3 );
for ( int i = my2DArray.GetLowerBound(0); i <= my2DArray.GetUpperBound(0); i++ )
for ( int j = my2DArray.GetLowerBound(1); j <= my2DArray.GetUpperBound(1); j++ )
my2DArray.SetValue( "abc" + i + j, i, j );
// Displays the values of the Array.
Console.WriteLine( "The two-dimensional Array contains the following values:" );
PrintValues( my2DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The two-dimensional Array contains the following values:
abc00 abc01 abc02
abc10 abc11 abc12
*/
์ค๋ช
๋๋ถ๋ถ์ ํด๋์ค์ Array ๋ฌ๋ฆฌ ๊ณต์ฉ ์์ฑ์ ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ CreateInstance ๋ฐํ์์ ๋ฐ์ธ๋ฉ๋ ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
์ฐธ์กฐ ํ์ ์์๊ฐ .๋ก null์ด๊ธฐํ๋ฉ๋๋ค. ๊ฐ ํ์ ์์๋ 0์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
์ด ๋ฉ์๋๋ O(n) ์ฐ์ฐ์ด๋ฉฐ, ์ฌ๊ธฐ์ ๊ณฑ length1 ์ ๋ค์๊ณผ n ๊ฐ์ต๋๋คlength2.
F#์์๋ Array2D.zeroCreate ํจ์๋ฅผ ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ ์ฉ ๋์
.NET 7 RC 1 ๋ฐ ๊ธฐํ ๋ฒ์ CreateInstance(Type, Int32[], Int32[])
public static Array CreateInstance (Type elementType, int[] lengths, int[] lowerBounds);
๋งค๊ฐ ๋ณ์
๋ง๋ค Array ์ ๊ฐ ์ฐจ์ ํํ(์์ ์ธ๋ฑ์ค)์ ํฌํจํ๋ 1์ฐจ์ ๋ฐฐ์ด์ ๋๋ค.
๋ฐํ
๊ฐ ์ฐจ์์ ๋ํด ์ง์ ํ ๊ธธ์ด ๋ฐ ํํ์ ๊ฐ์ง ์ง์ ํ Array ์ ์ ๋ค์ฐจ์ Type ์ ๋๋ค.
์์ธ
elementType์ด(๊ฐ) null์ธ ๊ฒฝ์ฐ
๋๋
lengths์ด(๊ฐ) null์ธ ๊ฒฝ์ฐ
๋๋
lowerBounds์ด(๊ฐ) null์ธ ๊ฒฝ์ฐ
elementType์ ์ ํจํ Type์ด ์๋๋๋ค.
๋๋
lengths ๋ฐฐ์ด์ 1๊ฐ ๋ฏธ๋ง์ ์์๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
๋๋
lengths์ lowerBounds ๋ฐฐ์ด์ ๋ค์ด ์๋ ์์ ์๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ
elementType์ ์ง์๋์ง ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด Void๋ ์ง์๋์ง ์์ต๋๋ค.
๋๋
elementType์ด ๊ฐ๋ฐฉํ ์ ๋ค๋ฆญ ํ์์ธ ๊ฒฝ์ฐ.
lengths์ ๊ฐ์ด 0๋ณด๋ค ์์ต๋๋ค.
๋๋
lowerBounds ์ฐจ์์ ํํ ๋ฐ ๊ธธ์ด ํฉ๊ณ๊ฐ Int32.MaxValue๋ณด๋ค ํฌ๋ฉด ๊ฐ์ด ๋งค์ฐ ํฝ์ต๋๋ค.
์์
๋ค์ ์ฝ๋ ์์ ์์๋ ์ง์ ๋ ํํ์ ์ฌ์ฉํ์ฌ ๋ค์ฐจ์ Array ์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ ์ค๋๋ค.
using System;
public class SamplesArray4 {
public static void Main() {
// Creates and initializes a multidimensional Array of type string.
int[] myLengthsArray = new int[2] { 3, 5 };
int[] myBoundsArray = new int[2] { 2, 3 };
Array myArray=Array.CreateInstance( typeof(string), myLengthsArray, myBoundsArray );
for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
int[] myIndicesArray = new int[2] { i, j };
myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
}
// Displays the lower bounds and the upper bounds of each dimension.
Console.WriteLine( "Bounds:\tLower\tUpper" );
for ( int i = 0; i < myArray.Rank; i++ )
Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );
// Displays the values of the Array.
Console.WriteLine( "The Array contains the following values:" );
PrintValues( myArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Bounds: Lower Upper
0: 2 4
1: 3 7
The Array contains the following values:
23 24 25 26 27
33 34 35 36 37
43 44 45 46 47
*/
์ค๋ช
๋๋ถ๋ถ์ ํด๋์ค์ Array ๋ฌ๋ฆฌ ๊ณต์ฉ ์์ฑ์ ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ์ฌ CreateInstance ๋ฐํ์์ ๋ฐ์ธ๋ฉ๋ ์ก์ธ์ค๋ฅผ ํ์ฉํฉ๋๋ค.
lengths ๋ฐฐ์ด๊ณผ lowerBounds ๋ฐฐ์ด์ ๊ฐ์๋ ๊ฐ์์ผ ํฉ๋๋ค. ๋ฐฐ์ด์ ์์ lengths ์๋ ์ Array์ฐจ์์ ์์ ๊ฐ์์ผ ํฉ๋๋ค.
๋ฐฐ์ด์ lengths ๊ฐ ์์๋ ์ Array์ฐจ์์ ๊ธธ์ด๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
๋ฐฐ์ด์ lowerBounds ๊ฐ ์์๋ ์ Array์ฐจ์์ ํํ์ ์ง์ ํด์ผ ํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก .NET ํด๋์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ง์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ 0์ด ์๋ ํํ์ ์ฒ๋ฆฌํ์ง ์์ต๋๋ค.
์ฐธ์กฐ ํ์ ์์๊ฐ .๋ก null์ด๊ธฐํ๋ฉ๋๋ค. ๊ฐ ํ์ ์์๋ 0์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
์ด ๋ฉ์๋๋ O(n) ์ฐ์ฐ์ ๋๋ค. ์ฌ๊ธฐ์ n ๋ชจ๋ ๊ฐ์ ๊ณฑ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค lengths.
์ฐธ๊ณ
๋ชจ๋ ์ธ์ด๊ฐ 0์ด ์๋ ํํ์ด ์๋ ๋ฐฐ์ด์ ์ง์ํ์ง ์์ผ๋ฏ๋ก ์ธ์ด์ ๋ฐฐ์ด ํ์์ ๋ฐ๋ผ Array 0์ด ์๋ ์ธ์คํด์ค๋ฅผ ์บ์คํ ํ์ง ๋ชปํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ํํ์ด 6์ธ 1์ฐจ์ ์ ์ ๋ฐฐ์ด์ C#์ int[] ํ์์ผ๋ก ์บ์คํ ํ ์ ์์ต๋๋ค. InvalidCastException ๊ทธ๋ฌ๋ฉด ๋ฐํ์ ์ค์ "'System.Int32[]' ํ์์ ๊ฐ์ฒด๋ฅผ 'System.Int32[*]' ํ์์ผ๋ก ์บ์คํ ํ ์ ์์ต๋๋ค."๋ผ๋ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค. ์ฌ๊ธฐ์ ๋ณํ(*)๋ 0์ด ์๋ ์ธ๋ฑ์ค๋ฅผ ์๋ฏธํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ธ์ด์ ๋ฐฐ์ด๋ก ๋ง๋ CreateInstance(Type, Int32[], Int32[]) ๋ชจ๋ ์์์ 0๋ถํฐ ์์ํ๋ ๋ฐฐ์ด์ ์บ์คํ ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ง๋ 2์ฐจ์ 0 ๊ธฐ๋ฐ ์ ์ ๋ฐฐ์ด์ C#์ int[,] ํ์์ผ๋ก ์บ์คํ ํ ์ ์์ต๋๋ค.
์ ์ฉ ๋์
.NET 7 RC 1 ๋ฐ ๊ธฐํ ๋ฒ์ CreateInstance(Type, Int32, Int32, Int32)
public static Array CreateInstance (Type elementType, int length1, int length2, int length3);
์์
๋ค์ ์ฝ๋ ์์ ์์๋ 3์ฐจ์ Array์ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
using System;
public class SamplesArray2 {
public static void Main() {
// Creates and initializes a three-dimensional Array of type Object.
Array my3DArray=Array.CreateInstance( typeof(Object), 2, 3, 4 );
for ( int i = my3DArray.GetLowerBound(0); i <= my3DArray.GetUpperBound(0); i++ )
for ( int j = my3DArray.GetLowerBound(1); j <= my3DArray.GetUpperBound(1); j++ )
for ( int k = my3DArray.GetLowerBound(2); k <= my3DArray.GetUpperBound(2); k++ )
my3DArray.SetValue( "abc" + i + j + k, i, j, k );
// Displays the values of the Array.
Console.WriteLine( "The three-dimensional Array contains the following values:" );
PrintValues( my3DArray );
}
public static void PrintValues( Array myArr ) {
System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
int i = 0;
int cols = myArr.GetLength( myArr.Rank - 1 );
while ( myEnumerator.MoveNext() ) {
if ( i < cols ) {
i++;
} else {
Console.WriteLine();
i = 1;
}
Console.Write( "\t{0}", myEnumerator.Current );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
The three-dimensional Array contains the following values:
abc000 abc001 abc002 abc003
abc010 abc011 abc012 abc013
abc020 abc021 abc022 abc023
abc100 abc101 abc102 abc103
abc110 abc111 abc112 abc113
abc120 abc121 abc122 abc123
*/
์ค๋ช
๋๋ถ๋ถ์ ํด๋์ค์ Array ๋ฌ๋ฆฌ, ๋ฐํ์์ CreateInstance ๋ฐ์ธ๋ฉ๋ ์ก์ธ์ค๋ฅผ ํ์ฉํ๊ธฐ ์ํด ๊ณต์ฉ ์์ฑ์ ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ฐธ์กฐ ํ์ ์์๊ฐ .๋ก null์ด๊ธฐํ๋ฉ๋๋ค. ๊ฐ ํ์ ์์๋ 0์ผ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.
This method is an O(n) operation, where n is the product of length1, length2, and length3.
F#์์ Array3D.zeroCreate ํจ์๋ฅผ ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
'C# > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C#] ์ ์ ์ซ์ ํ์ (0) | 2022.06.11 |
---|---|
ํด๋์ค๋ฅผ ๋ฐฐ์ด๋ก ์ ์ธํ๊ธฐ (0) | 2022.02.24 |
ํ๋, ๋ฉค๋ฒ, ์ง์ญ๋ณ์, ์ ์ ํ๋, ์ ์ ๋ฉ์๋ ๊ฐ๋ ์ ๋ฆฌ (0) | 2022.02.14 |
[C#]๋ฌธ์์ด ์ถ๋ ฅ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ (0) | 2022.02.04 |
[C# ์ฝ์] 5x5 ๋๊ทธ๋ผ๋ฏธํ ์ถ๋ ฅ (0) | 2022.02.02 |