반응형
SMALL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class App
{
public App()
{
Console.WriteLine("App 생성자 입니다.");
//배열
//연속된 같은 데이터를 관리할수 있는 공간
//배열 변수 정의
//타입[] 변수명
//정수형 테이터들을 저장하고 싶다면
int[] scores;
//배열 인스턴스를 생성
//생성된 배열은 반드시 할당을 해주어야 한다.
scores = new int[5]; //배열 초기화
//배열의 요소(변수) 인덱스 0~...배열의 용량 -1
//인덱스로 접근이 가능
//scores[-1] = 10; //배열의 인덱스는 무족껀0부터 시작함 = -1 은 있을수 없음
scores[0] = 10; //배열의 인덱스는 무족껀0부터 시작함.
scores[1] = 10;
scores[2] = 10;
scores[3] = 10;
scores[4] = 10;
//scores[5] = 1000; //얘는 공간 자체가 없는 것으로 반환 안함. (*index out of range) = 에러
// 인벤토리 만드는게 가능
//Console.WriteLine(scores[0]);
//Console.WriteLine(scores[1]);
//Console.WriteLine(scores[2]);
//Console.WriteLine(scores[3]);
//Console.WriteLine(scores[4]);
//for(int i = 0; i < scores.Length; i++) // i : 6
//{
// Console.WriteLine(scores[i]); // 0,1,2
//}
foreach ( int score in scores)
{
Console.WriteLine(score);
}
}
}
}
1~10 합치기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class App
{
public App()
{
Console.WriteLine("App 생성자 입니다.");
//1부턱 10까지의 합을 출력하세요.
//for 문의 사용하여
//int[] numbers;
//numbers = new int[11];
////1~10까지 더하기
//int sum = 0; //외부에 초기화한 숫자를 정해줌
//for(int i =0; i < 10; i++) // 더하고싶은 숫자까지 i 값을 지정해주고
//{
// sum = sum + i;
//}
//Console.WriteLine(sum);
int[] scores = new int[3];
scores[0] = 10;
scores[1] = 20;
scores[2] = 30;
int sum = 0;
foreach (int score in scores) // 해당요소의 값을 읽을때만 사용해야함.
{
sum = sum + score;
}
Console.WriteLine(sum);
}
}
}
초기화 다른 방법 추가
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class App
{
public App()
{
Console.WriteLine("App 생성자 입니다.");
int[] arr = { 1,2,3 };
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
arr[0] = 100;
arr[1] = arr[2];
arr = null;
Console.WriteLine(arr[0]); //arr 인스턴스의 요소에 접근할수 없기 때문에
}
}
}
초기화 + for문 사용 + foreach 사용
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class App
{
public App()
{
Console.WriteLine("App 생성자 입니다.");
//문자열 배열 fruitNames 변수를 정의하고
string[] fruitNames = { "사과", "바나나", "수박" };
//배열 인스턴스를 생성하세요
//배열의 초기값을 "사과","바나나","수박"
//으로 설정합니다.
//for문을 사용해서 배열의 요소를 모두 출력하세요
//foreach문을 사용해서 배열의 요소를 모두 출력하세요
for (int i = 0; i < fruitNames.Length; i++)
{
string fruitName = fruitNames[i];
Console.WriteLine(fruitName);
}
foreach (string fruitName in fruitNames)
{
Console.WriteLine(fruitName);
}
}
}
}
null 값 오류 확인하기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study06
{
class App
{
public App()
{
Console.WriteLine("App 생성자 입니다.");
Item[] itmes = new Item[5];
itmes[0] = new Item("장검");
itmes[1] = new Item("단검");
itmes[2] = new Item("지팡이");
for(int i = 0; i < itmes.Length; i++)
{
if (itmes[i] != null) //5번까지 돌기때문에 itemes.name 을 3번쨰 부터는
Console.WriteLine(itmes[i].name);
}
//itmes 배열의 요소의 마지막 인덱스 까지 계속 집어넣는다.
foreach (Item item in itmes) //타입을 작성할때 배열의 인스턴스 타입을 신경써서 본다.
{
if (item != null)
Console.WriteLine(item.name);
}
}
}
}
반응형
LIST
'C# > 수업 내용' 카테고리의 다른 글
배열 안에 있는 개수 세기 (0) | 2023.01.09 |
---|---|
2023.01.09 오전 배열 복습 (0) | 2023.01.09 |
코인획득 (클래스 반환 이용) (0) | 2023.01.06 |
상속 클래스 (다운캐스팅 업캐스팅) + as연산자 (0) | 2023.01.05 |
시즈모드 변형시키기 (class이용) (0) | 2023.01.05 |