본문 바로가기
KDT/C# 프로그래밍

23/07/25 배열 복습2

by 잰쟁 2023. 7. 25.
728x90
using Starcraft;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class App
    {
        

        //생성자 메서드
        public App()
        {
            //아이템 배열변수 items를 정의하고 배열의 인스턴스를 생성하고 할당
            Item[] items = new Item[3];
            Console.WriteLine(items);  //배열 인스턴스
            Console.WriteLine(items.Length);  //3

            //인덱스로 배열의 요소에 접근해서 값을 할당
            //값은 어떤 타입만 넣을 수 있는가? Item
            //배열: 같은 타입의 값들을 그룹화 하고 관리하는 컨테이너
            items[0] = new Item();
           //배열을 순회
           for (int i = 0; i < items.Length; i++)
            {
                Console.WriteLine(items[i]); //Item,null,null
            }
           //foreach문으로 배열 순회
           foreach(Item item in items)
            {
                Console.WriteLine(item); //Item,null,null
            }

           //빈배열을 만들어서
            Item[] tempArr = new Item[3];
            for (int i = 0;i < items.Length;i++) 
            {
                tempArr[i] = items[i];   //items에 있는 요소의 값을 옮겨 담는다
            }

        }
    }
}