본문 바로가기
C#/수업내용

[C# 6일차] 배열

by 왹져박사 2023. 1. 6.
728x90
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 생성자");

            //배열
            //같은 데이터를 관리하는 공간(배열의 인스턴스)

            //배열 변수 정의
            //데이터형식[] 배열이름(변수명) = new 데이터형식[배열크기];
            //배열이름은 복수형/앞에 arr붙임

            int[] scores;
            //배열 인스턴스 생성
            scores = new int[5];

            //배열의 요소 인덱스 0~(배열의 용량-1)
            //인덱스로 접근이 가능

            scores[0] = 101;
            scores[1] = 102;
            scores[2] = 103;
            scores[3] = 104;
            scores[4] = 105;

            //배열의 인덱스를 벗어나면 에러

            Console.WriteLine(scores[0]);

            int score0 = scores[0];

            for(int i = 0; i < scores.Length; i++)
            {
                Console.WriteLine(scores[i]);
            }
            Console.WriteLine();

            //foreach(데이터형식 변수이름 in 배열이름)       //배열에서만 사용 가능
            foreach(int score in scores)        //다음 인덱스가 없을때까지 실행
            {
                Console.WriteLine(score);
            }
        }
    }
}
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 생성자");

            //문자열형식 배열 변수(names)를 정의하세요.
            string[] names;

            //문자열형식 배열 인스턴스 (용량:5)를 생성하고
            //names에 할당하세요
            names = new string[5];

            //배열의 2 인덱스에 값 "홍길동"을 할당하세요
            names[2] = "홍길동";

            //배열의 2 인덱스 값을 출력하세요
            Console.WriteLine(names[2]);
            Console.WriteLine();

            //for문을 사용해 배열의 모든 요소를 출력하시고
            //만약 요소의 값이 없다면 -를 출력하세요
            for(int i = 0; i < names.Length; i++)
            {
                if (names[i] == null)
                {
                    Console.WriteLine("-");
                }
                else
                {
                    Console.WriteLine(names[i]);
                }
            }

            Console.WriteLine();

            //1부터 10까지의 합을 출력하세요
            //for문을 사용하세요
            int sum = 0;
            for(int i = 1; i <= 10; i++)
            {
                sum = sum + i;
            }
            Console.WriteLine(sum);
            Console.WriteLine();


        }
    }
}
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 = new int[5];
            scores[0] = 80;
            scores[1] = 74;
            scores[2] = 81;
            scores[3] = 90;
            scores[4] = 34;

            int sum = 0;
            foreach (int score in scores)       //요소의 값을 읽을때만 사용해야 함 (변경, 제거x _for문을 써야 함)
            {
                sum = sum + score;
            }
            Console.WriteLine(sum);

            Console.WriteLine();


        }
    }
}
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;          //인스터스 값이기 때문에 null 저장 가능
            //Console.WriteLine(arr[0]);      //배열이 null이어서 생기는 문제x!!! 배열의 0번째 요소 멤버에 접근하려고 했기 때문에 에러.


            Console.WriteLine();


        }
    }
}
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 생성자");
            Console.WriteLine();


            //문자열 배열 fruitsNames 변수를 정의하고
            //배열 인스턴스를 생성하세요
            //배열의 초기 값을 "사과", "바나나", "수박"
            //으로 설정합니다
                string[] fruitsNames = { "사과", "바나나", "수박" };       //암시적으로 초기값을 설정하면 후에 초기화 불가능


            //for문을 사용해서 배열의 요소를 모두 출력하세요
            for(int i = 0; i < fruitsNames.Length; i++)
            {
                Console.WriteLine(fruitsNames[i]);
            }
            Console.WriteLine();

            //foreach문을 사용해서 배열의 요소를 모두 출력하세요
            foreach (string fruitsName in fruitsNames)
            {
                Console.WriteLine(fruitsName);
            }

            Console.WriteLine();
        }
    }
}
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 생성자");
            Console.WriteLine();

            int[] arr = new int[3] { 1, 2, 3 };

            //method overloading: 매개변수만 다르게 사용
            int index = Array.IndexOf(arr, 2);      //.앞에 클래스!    method 앞에 static을 붙이면 Class의 형식으로 지정.
                                                                    //장점: 인스턴스를 만들지 않아도 사용 가능, 단점: 항상 메모리를 잡아먹음
            Console.WriteLine(index);

            Console.WriteLine();
        }
    }
}

 

주의!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 생성자");
            Console.WriteLine();

            Item[] items = new Item[5];
            items[0] = new Item("장검");
            items[1] = new Item("단검");
            items[2] = new Item("지팡이");

            //for문을 사용해 Item이름을 출력하세요
            for(int i = 0; i < items.Length; i++)
            {
                if(items[i] == null)
                {
                    continue;
                }
                else
                {
                    Console.WriteLine(items[i].name);
                }
            }

            //foreach문을 사용해 Item이름을 출력하세요
            foreach (Item item in items)
            {
                if(item == null)
                {
                    continue;
                }
                else
                    Console.WriteLine(item.name);
            }
        }
    }
}
728x90