[C#] 7. ๋ฐฐ์—ด

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,




C# ์‚ฌ์ „ ๋ฌธ๋ฒ• ๊ธฐ์ดˆ



๋ฐฐ์—ด








1. ๊ธฐ๋ณธ

๋ฐฐ์—ด์ด๋ž€ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๊ฐ€์ง€ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ

image

string[] game;
  • ๋ฐฐ์—ด์ด๋ž€ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๊ฐ€์ง€ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ








2. ์ƒ์„ฑ

๋ฐฐ์—ด์˜ ์„ ์–ธ ๊ตฌ์กฐ
image

// ๊ธฐ๋ณธ์ ์ธ ๋ณ€์ˆ˜ 
string game;

// ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐ”๋กœ ๋ฐฉ์„ ๋งŒ๋“ค๊ธฐ
string[] game = new string[3];

// ๋ณ€์ˆ˜๋งŒ ๋งŒ๋“ค์–ด ๋‘๊ณ  ๋ฐฉ์€ ๋‚˜์ค‘์— ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
string[] game;
game = new string[3];
  • ๋ฐฐ์—ด์€ ๋ฐ์ดํ„ฐ ํ•˜๋‚˜๊ฐ€ ์•„๋‹Œ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋Š” ์ง‘
  • ์ฒ˜์Œ๋ฐฐ์—ด์€ 0๋ถ€ํ„ฐ game[0], game[1], game[2] ๊ฐ€ ์กด์žฌํ•œ๋‹ค.








3. ๋ฐ์ดํ„ฐ ์ €์žฅ

// game์˜ 1ํ˜ธ์‹ค์— โ€œ๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌโ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
string[] game = new string[3];
game[0] = "Leage of Legends"
game[1] = "๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌ"
  • image
  • ๋Œ€๊ด„ํ˜ธ [ ] ์™€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ํ™œ์šฉ








4. ๋ฐ์ดํ„ฐ ํ™œ์šฉ

string[] game = new string[3];
game[0] = "Leage of Legends"
game[1] = "๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌ"

//ํ™œ์šฉ
Console.WriteLine(game[0]); // ์ถœ๋ ฅ - League of Legends 
Console.WriteLine(game[1]); // ์ถœ๋ ฅ - ๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌ
  • ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ / ํ™œ์šฉ ํ•  ๋•Œ ๋Œ€๊ด„ํ˜ธ [ ] ์™€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉ








5. ๊ฐ ํƒ€์ž…๋ณ„ ๋ฐฐ์—ด

๋ฐฐ์—ด์€ ๋ฌธ์ž์—ด ๋ง๊ณ ๋„ ํƒ€์ž…์— ๋งž๋Š” ๋ฐฐ์—ด๋“ค์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

//์ •์ˆ˜ ๋ฐฐ์—ด
int[] year = new int[2];
year[0] = 2022;
year[1] = 2023;


Console.WriteLine(year[0]);  // ์ถœ๋ ฅ - 2022
Console.WriteLine(year[1]);  // ์ถœ๋ ฅ - 2023

//์‹ค์ˆ˜ ๋ฐฐ์—ด
float[] height = new float[4];
height[0] = 164.5f;
height[1] = 172.7f;
height[2] = 181.2f;

Console.WriteLine(height[0]);  // ์ถœ๋ ฅ - 164.5
Console.WriteLine(height[1]);  // ์ถœ๋ ฅ - 172.7
Console.WriteLine(height[2]);  // ์ถœ๋ ฅ - 181.2
  • ๋ฐฐ์—ด์˜ ํƒ€์ž…์ด ๋งž์ง€ ์•Š๋Š” ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค








6. ๋ฐ˜๋ณต์ฒ˜๋ฆฌ

for ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ์ €์žฅ, ์ถœ๋ ฅ์„ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

int[] year = new int[4]
for(int i = 0 ; i < 4 ; i++)
{
	year[i] = 2020 + i;			//์ €์žฅ
	Console.WriteLine(year[i]);	//์ถœ๋ ฅ
}

์‹คํ–‰๊ฒฐ๊ณผ
2020
2021
2022
2023








7. foreach

๊ตฌ์„ฑ
image
์กฐ๊ฑด์‹์„ ์“ฐ์ง€ ์•Š๊ณ  ํ•ด๋‹น ๋ฐฐ์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ• ๋•Œ ์‚ฌ์šฉ

                               // ๋ฐฐ์—ด์˜ ํƒ€์ž…๊ณผ ์ผ์น˜
string[] games = new string[3] {"League of Legends", "๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌ", "๋””์•„๋ธ”๋กœ"} 
foreach(string game in games)
{
		Console.WriteLine(game);
}

์‹คํ–‰๊ฒฐ๊ณผ
League of Legends
๋ฉ”์ดํ”Œ ์Šคํ† ๋ฆฌ
๋””์•„๋ธ”๋กœ
  • ์ง€์ •ํ•  ๋ฐ˜๋ณต๋ณ€์ˆ˜์˜ ํƒ€์ž…์€ ๋ฐฐ์—ด์˜ ํƒ€์ž…๊ณผ ์ผ์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.








8. ์ •๋ฆฌ, ์ถ”๊ฐ€

๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ์„ ์–ธํ›„ ์ €์žฅํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ๋‹ค.



[C#] ๋ฐฐ์—ด


์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ