728x90
반응형

[예제1]

배열 90도 회전

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {            
            int[,] ar = new int[3,4]
                {
                {8, 3, 2, 7},
                {5, 6, 2, 9}, 
                {3, 8, 1, 6 }
                }; // 세로, 가로
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Write(ar[i, j] + " ");
                }
                WriteLine();
            }


            int[,] br = new int[4, 3];
            for (int i = 0; i < 4; i++)

            {
                for (int j = 0; j < 3; j++)
                {
                    br[i, j] = ar[2 - j, i];
                    Write(br[i, j] + " ");
                }
                WriteLine();
            }            
            WriteLine();           
        } 
    }
}


[예제2]

2차배열(2중for문)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        	// int[,] ar = new int[2,3]
            int[,] ar = new int[,]
            {
                {1, 2, 3},
                {4, 5, 6}
            };
            WriteLine(ar.GetLength(0)); // 2
            WriteLine(ar.GetLength(1)); // 3
            WriteLine();

            for (int i = 0; i < ar.GetLength(0); i++)
            {
                for (int j = 0; j < ar.GetLength(1); j++)
                {
                    Write("[{0}, {1}]", i, j);
                }WriteLine();
            }WriteLine();

            for (int i = 0; i < ar.GetLength(0); i++)
            {
                for (int j = 0; j < ar.GetLength(1); j++)
                {
                    Write(ar[i, j] + " ");
                }
                WriteLine();
            }
            WriteLine();
        } 
    }
}


[예제3]

3차배열(3중for문)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
        	// int[,,] ar = new int[2,3,4]
            int[,,] ar = new int[,,]
            {
                {
                    {1, 2, 3, 4 },
                    {2, 3, 4, 5 },
                    {3, 4, 5, 6 }
                },
                {
                    { 4, 5, 6, 7 },
                    { 5, 6, 7, 8 },
                    { 6, 7, 8, 9 }
                }
            };
            WriteLine(ar.GetLength(0)); // 2
            WriteLine(ar.GetLength(1)); // 3
            WriteLine(ar.GetLength(2)); // 4
            WriteLine();

            for (int i = 0; i < ar.GetLength(0); i++)
            {
                for (int j = 0; j < ar.GetLength(1); j++)
                {
                    for (int k = 0; k < ar.GetLength(2); k++)
                    {
                        Write("[{0}, {1}, {2}]", i, j, k);
                    }
                    WriteLine();
                }
                WriteLine();
            }
            WriteLine();

            for (int i = 0; i < ar.GetLength(0); i++)
            {
                for (int j = 0; j < ar.GetLength(1); j++)
                {
                    for (int k = 0; k < ar.GetLength(2); k++)
                    {
                        Write(ar[i, j, k] + " ");
                    }
                    WriteLine();
                }
                WriteLine();
            }
            WriteLine();
        }
    }
}


[예제4]

2차원배열, 가변배월

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // O: 길이를 알고있을때, X: 길이를 모를때
            // int p1[3][4]; // 가로 O 세로 O
            // int* p1[3];   // 가로 O 세로 X
            // int** p1;     // 가로 X 세로 X
            // int(* p1)[4]  // 가로 X 세로 O (세로길이 몰라)

            // 결과
            // int p1[3[4];

            // 2차원 배열
            // int[,] ar = new int[3,4];

            // 가변 배열
            // 세로 길이는 알겠다.(3개)
            int[][] ar = new int[3][];
            ar[0] = new int[3] { 0, 1, 2 };
            ar[1] = new int[4] { 3, 4, 5, 6 };
            ar[2] = new int[5] { 7, 8, 9, 8, 7 };
            WriteLine(ar[0][0]); //0
            WriteLine(ar[2][4]); //7
            WriteLine("====가변배열====");

            //int[] br = new int[3];
            //foreach (var t in br){}
            foreach(int[] item in ar)
            {
                //WriteLine(item);
                foreach(var data in item)
                {
                    Write(data + " ");
                }WriteLine();
            }
        }
    }
}


[예제5]

ArrayList

using System;
using System.Collections.Generic;
using System.Collections; // ArrayList 사용하기 위해
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList li = new ArrayList();
            for (int i = 0; i < 5; i++)
            {
                li.Add(i * 5);            
            }

            foreach(var item in li)
            {
                Write(item + " ");
            }
            WriteLine();

            // 인수는 index
            li.RemoveAt(2);
            foreach (var item in li)
                Write(item + " ");
            WriteLine();
            // 결과
            // 0 5 99 15 20

            // 현재 컨테이너 표시가 없다.
            li.Add("호랑이");
            li.Add("코끼리");
            foreach (var item in li)
                Write(item + " ");
            WriteLine();

            // 문법체크
            Add(100);
            Add("호랑이");
        }
        // var 타입은 함수 파라미터로 사용할 수 없다.
        // public static void f1(var a) {}
        public static void Add(object a)
        { }
    }
}


[예제6]

using System;
using System.Collections.Generic;
using System.Collections; // ArrayList 사용하기 위해
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Queue li = new Queue();
            li.Enqueue(1);
            li.Enqueue(2);
            li.Enqueue(3);
            li.Enqueue(4);
            li.Enqueue(5);
            WriteLine(li.Count);
            li.Dequeue();
            Object data = li.Dequeue();
            WriteLine(data);
            WriteLine(li.Count);
            foreach(var item in li)
            {
                WriteLine(li + " ");
            }WriteLine();
        }        
    }
}


[예제7]

Hashtable

해쉬 테이블은 저장을 순차적으로 하지 않는다. 

using System;
using System.Collections.Generic;
using System.Collections; // ArrayList 사용하기 위해
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {                       
            Hashtable ht = new Hashtable();
            ht[0] = "호랑이";
            ht[1] = "코끼리";
            ht[2] = "독수리";
            ht["하나"] = "소나무";
            ht["둘"] = "중나무";
            ht["셋"] = "대나무";
            // Add(key, value)
            ht.Add("넷", "왕나무");            
            WriteLine(ht[1] + " " + ht["둘"]);
            foreach(DictionaryEntry item in ht)
            {
                Write(item.Key + " " + item.Value + " ");
            }WriteLine();
            WriteLine(ht.Count);      
        }        
    }
}

728x90
반응형

+ Recent posts