arr.cs

Seungbin Yang / 양승빈·2024년 4월 2일

비주얼프로그래밍

목록 보기
9/21

코드

namespace _013_arr
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 정수배열 {1,2,3} 만들기
            int[] a = { 1, 2, 3 };  // 만들면서 초기화
            int[] b = new int[3] { 1, 2, 3 };   // 3개짜리 정수배열을 만들고나서 초기화
            int[] c = new int[] { 1, 2, 3 };
            int[] d = new int[3];
            d[0] = 1;
            d[1] = 2;
            d[2] = 3;

            Console.Write("a[] : ");
            foreach (int i in a) {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            Console.Write("b[] : ");
            foreach (int i in b){
                Console.Write(i + " ");
            }
            Console.WriteLine();

            Console.Write("c[] : ");
            foreach (int i in c) {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            Console.Write("d[] : ");
            foreach (int i in d) {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            string[] s = { "abc", "bcd", "cde" };
            Console.Write("s[] : ");
            foreach (string i in s)
                Console.Write(i + " ");
            Console.WriteLine();

            // List<T>
            List<int> la = new List<int>();
            la.Add(10);
            la.Add(20);
            la.Add(3);

            la.Sort();
            Console.Write("List<add> la : ");
            foreach (int i in la) {
                Console.Write(i + " ");
            }
            Console.WriteLine();
        }
    }
}

출력 결과

int[] a = { 1, 2, 3 }; 형태로 배열을 생성할 수 있다.

0개의 댓글