다차원배열
1차원배열
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
//배열의 인스턴스화
int[] arr = new int[3]; //3:length
int[] arr2 = { 1, 2, 3 };//3
//인덱스로 배열의 요소에 접근
arr[0] = 1;
Console.WriteLine(arr[0]);
//배열의 순회 (요소를 출력)
for(int i=0;i<arr.Length;i++)
{
Console.WriteLine(arr[i]);
}
foreach(int num in arr)
{
Console.WriteLine(num);
}
}
}
}
https://www.youtube.com/watch?v=oTgru_A31Ss&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
//배열의 인스턴스화
int[,] arr = new int[3, 2];
//인덱스로 배열의 요소에 접근
arr[1, 0] = 1;
//배열의 순회 (요소를 출력)
int rowLength = arr.GetLength(0);
//3
int colLength = arr.GetLength(1);
//2
for(int i = 0; i < rowLength; i++)
{
for(int j=0;j < colLength; j++)
{
Console.Write("{0},{1}({2}) ", i, j, arr[i,j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//생성자
public App()
{
//배열의 인스턴스화
int[,] arr = new int[2, 3];//행:row, 열: col
//인덱스로 배열의 요소에 접근
arr[0, 2] = 1;
arr[1, 1] = 1;
//배열의 순회 (요소를 출력)
int rowLength = arr.GetLength(0);//행 길이
//3
int colLength = arr.GetLength(1);//열 길이
//2
for(int i = 0; i < rowLength; i++)
{
for(int j=0;j < colLength; j++)
{
Console.Write("[{0},{1}] : {2} ", i, j, arr[i,j]);
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
//vector2 :구조체
public App() {
Vector2 position = new Vector2(2, 1); //좌표
Console.WriteLine(position.ToString());
Vector2 indices = Utils.ConvertPosition2Indices(position);
Console.WriteLine(indices.ToString());
position = Utils.ConvertIndices2Position(indices);
Console.WriteLine(position.ToString());
// Vector2 indices = new Vector2(0, 0);//배열의 인덱스
// Console.WriteLine(indices.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
//구조체:값형식 - > 스택에 저장됨
//기본생성자 못씀
//상속부락, 기본클래스로 못씀
//인터페이스 사용가능
internal class Vector2
{
public int x;
public int y;
//생성자
public Vector2(int x,int y)
{
this.x = x;
this.y = y;
}
//부모클래스의 virtual 멤버 메서드 재정의
public override string ToString()
{
// return string.Format("({0},{1}),this.x,this.y");
return $"({this.x}.{this.y})";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Utils
{
//static은 정적 메서드. 프로그램 시작시 가장 먼저 메모리에 올라가서
//프로그램 종료시까지 살아있으며, static 끼리만 접근가능
//**static은 인스턴스로 접근 불가 타입으로 접근가능.
public static Vector2 ConvertPosition2Indices(Vector2 position)
{//좌표 -> 인덱스로
return new Vector2(position.y, position.x);
}
public static Vector2 ConvertIndices2Position(Vector2 indices)
{
return new Vector2(indices.y, indices.x);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
int[,] map = new int[4, 3];
//vector2 :구조체
public App() {
this.PrintMap();
}
//맵을 출력
void PrintMap()
{
for(int i = 0; i < map.GetLength(0); i++)
{
for(int j = 0; j < map.GetLength(1); j++)
{
Console.Write("{0} ", map[i, j]);
}
Console.WriteLine();
}
}
}
}
<플레이어를 이동시켜 보는 2차원 배열>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
int[,] map = {
{1,1,2 ,1},
{1,1,1,1}
};
int[,] playerMap;
int rowIdx;
int colIdx;
//vector2 :구조체
//생성자
public App()
{
playerMap = new int[map.GetLength(0), map.GetLength(1)];
this.PrintMap(map);
this.PrintSpace();
this.PrintMap(playerMap);
this.PrintSpace();
//플레이어의 초기위치 설정
this.rowIdx = 1;
this.colIdx = 2;
playerMap[this.rowIdx, this.colIdx] = 100;
this.PrintMap(playerMap);
this.PrintSpace();
this.MoveLeft();
this.PrintSpace();
this.PrintMap(playerMap);
}
//맵을 출력
void PrintMap(int[,] arr)
{
for(int i = 0; i < arr.GetLength(0); i++)
{
for(int j = 0; j < arr.GetLength(1); j++)
{
Console.Write("{0} ", arr[i, j]);
}
Console.WriteLine();
}
}
void PrintSpace()
{
Console.WriteLine();
}
void MoveLeft()
{
playerMap[this.rowIdx, this.colIdx - 1] = 100;
playerMap[this.rowIdx, this.colIdx] = 0;
this.colIdx -= 1;
Console.WriteLine("왼쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
}
}
}
https://learn.microsoft.com/ko-kr/dotnet/api/system.object?view=net-7.0
(값형식은 valueType 클래스의 파생클래스
(->구조체도 값형식이므로 파생클래스로 볼수도 : 알아만두자.)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
//구조체 : 값형식 -> 스택에 저장됨
//(값형식은 valueType 클래스의 파생클래스
//(->구조체도 값형식이므로 파생클래스로 볼수도 : 알아만두자.)
//기본 생성자 못씀
//상속 불가능, 기본클래스로 못씀 : 부모가 될 수 없음
//인터페이스 사용가능
internal struct Vector2
{
public int x;
public int y;
//생성자
public Vector2(int x, int y)
{
this.x = x;
this.y = y;
}
//부모 클래스의 virtual 멤버 메서드 재정의
//재정의할 때 override사용
//Object 클래스는 모든 .NET 클래스의 기본 클래스이므로 ToString()을 재정의해 사용
public override string ToString()
{
return string.Format("({0},{1})", this.x, this.y);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class App
{
public App()
{
Vector2 v = new Vector2(0, 0);
Console.WriteLine(v.ToString());
}
}
}
Utils 클래스 추가 및 클래스들 수정
배열 요소에 접근 -> 배열 인스턴스가 나온다.
가변 배열의 각 요소는 배열이다.
'C# 기초' 카테고리의 다른 글
가짜 인벤토리 만들기 (0) | 2023.07.26 |
---|---|
namespace, 컬렉션, (0) | 2023.07.26 |
배열 연습 (0) | 2023.07.25 |
배열 예제 (0) | 2023.07.24 |
구조체, 추상 클래스, 인터페이스, 1차원 배열 (0) | 2023.07.24 |