Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

행복한 개구리

C# 21.03.18.수업내용 본문

C#/수업내용

C# 21.03.18.수업내용

HappyFrog 2021. 3. 18. 12:25
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");
            //Stack<string> 변수 선언과 인스턴스 생성 및 초기화
            Stack<string> names = new Stack<string>();
            //요소 추가
            names.Push("질럿");
            names.Push("저글링");
            names.Push("드라군");
            names.Push("다크템플러");
            //foreach문을 사용하여 요소 호출
            foreach (string name in names)
            {
                Console.WriteLine(name);
            }
            //Count
            Console.WriteLine(names.Count);
            //Pop
            string popElement = names.Pop();
            Console.WriteLine(popElement);
            //Contains
            bool isContains = names.Contains("질럿");
            Console.WriteLine(isContains);
            //Peek
            string peekElement = names.Peek();
            Console.WriteLine("Peek : {0}", peekElement);

            //stack -> array    //요소복사본을 포함하는 새 배열 반환
            string[] arrNames;
            arrNames = names.ToArray();
            //for
            Console.WriteLine("======== Array ========");
            for(int i = 0; i<arrNames.Length; i++)
            {
                Console.WriteLine("index : {0}, names : {1}", i, arrNames[i]);
            }

            //foreach
            foreach(string name in names)
            {
                Console.WriteLine(name);
            }

            //copyTo
            Console.WriteLine("======== Array arrNames copy to arrNames2 ========");
            string[] arrNames2 = new string[arrNames.Length * 2];
            Console.WriteLine(arrNames2.Length);
            arrNames.CopyTo(arrNames2, arrNames.Length);        // A.CopyTo(x,y) = A배열의 요소들을 x배열에 y번째인덱스부터 채우겠다는 말.
            for (int i = 0; i < arrNames2.Length; i++)
            {
                if (string.IsNullOrEmpty(arrNames2[i]))
                {
                    Console.WriteLine("[EMPTY]");
                }
                else
                {
                    Console.WriteLine(arrNames2[i]);
                }                
            }

            //Clear = 모두ㅇ
            names.Clear();
            Console.WriteLine(names.Count);
        }
    }
}

 

Stack = 선입후출구조

Queue = 선입선출구조

 

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            //Queue<string> 인스턴스 생성
            Queue<string> names = new Queue<string>();

            //요소추가
            names.Enqueue("질럿");
            names.Enqueue("저글링");
            names.Enqueue("드라군");
            names.Enqueue("다크템플러");

            //foreach문으로 요소 출력
            foreach(string name in names)
            {
                Console.WriteLine(name);
            }

            //Dequeue
            string dequeueElement = names.Dequeue();
            Console.WriteLine(dequeueElement);

            string[] arrNames = names.ToArray();
            for(int i = 0; i<arrNames.Length; i++)
            {
                Console.WriteLine("인덱스 {0}, 이름 {1}", i, arrNames[i]);
            }
            Console.WriteLine("================ names copy to namesCopy ================");
            Queue<string> namesCopy = new Queue<string>(arrNames);
            //foreach문으로 요소 출력
            foreach(string name in names)
            {
                Console.WriteLine(name);
            }
            //Contains
            bool isContains = names.Contains("질럿");
            Console.WriteLine(isContains);

            //Clear
            names.Clear();
            Console.WriteLine(names.Count);
        }
    }
}

 

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");
            //List<Item>변수 선언
            List<Item> list;

            //List<Item> 객체 생성 및 할당
            list = new List<Item>();

            //Item 객체 생성
            Item item1 = new Item(200,"장검");
            Item item2 = new Item(201, "단검");
            Item item3 = new Item(202, "활");

            //List<Item> 요소 수 출력
            list.Add(item1);
            list.Add(item2);
            list.Add(item3);

            Console.WriteLine(list.Count);
            
            //for문으로 요소 출력 (id, name)
            for(int i = 0; i < list.Count; i++)
            {
                Item item = list[i];
                Console.WriteLine("{0}, {1}", item.Id, item.name);
            }

            //foreach문으로 요소 출력(id, name)
            foreach(Item item in list)
            {
                Console.WriteLine("{0}, {1}", item.Id, item.name);
            }

        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            //배열 변수 선언
            int[,] arr;
            //배열 인스턴스 선언 및 초기화
            arr = new int[,]
            {
                {1, 2 },
                {3, 4 },
                {6, 5 },
                {7, 8 },
            };
                       
            //차원의 배열 길이 출력
            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

            //배열의 요소 출력
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for(int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.WriteLine("arr[{0}, {1}]: {2}", i, j, arr[i, j]);
                }
            }

        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            Console.WriteLine("App");

            //배열 선언 및 초기화
            int[,] arr = new int[4,4]
            {
                {0,0,0,0 },
                {0,-1,-1,0 },
                {0,0,0,0 },
                {0,0,0,0 },
            };
            //배열의 각 차원 길이 출력
            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

            //배열의 요소 출력
            for(int i = 0; i < arr.GetLength(0); i++)
            {
                for(int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.WriteLine("({0},{1}), {2}", i, j, arr[i, j]);
                }
            }

        }
    }
}

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력
            Console.WriteLine("App");


            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            int[,] arr = new int[4, 3]
            {
                { 101,201,303},
                { 101,101,101},
                { 102,300,301},
                { 303,304,305},
            };

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

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

 

 

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력

            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] arr = new int[4, 4]
            {
                {200,200,200,307 },
                {200,200,306,305 },
                {200,200,303,304 },
                {300,301,302,200 },
            };

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

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

 

 

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[3, 5]
            {
                {200,200,100,100,100},
                {100,100,200,200,200},
                {100,200,200,200,200}
            };

            int[,] unit = new int[3, 5]
            {
                {0,0,0,0,0 },
                {0,0,1,0,0 },
                {0,0,0,0,0 },
            };

            Console.WriteLine("========= MAP =========");
            Console.WriteLine(map.GetLength(0));
            Console.WriteLine("*");
            Console.WriteLine(map.GetLength(1));
            Console.WriteLine("========= UNIT =========");
            Console.WriteLine(unit.GetLength(0));
            Console.WriteLine("*");
            Console.WriteLine(unit.GetLength(1));
            Console.WriteLine("=======================");


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


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

================================================

 

 

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[5, 4]
            {
                {200,200,100,100 },
                {100,100,200,200 },
                {100,200,200,200 },
                {200,200,200,200 },
                {301,301,302,200 },
            };

            int[,] character = new int[5, 4]
            {
                {0,0,0,0 },
                {0,0,1,0 },
                {0,0,0,0 },
                {0,0,0,0 },
                {0,0,0,0 },
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

            Console.WriteLine(character.GetLength(0));
            Console.WriteLine(character.GetLength(1));

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

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



        }
    }
}

================================================

 

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] arr = new int[3, 4]
            {
                {100,100,100,100 },
                {200,200,200,100 },
                {200,200,200,200 }
            };

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

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

            }
            Console.WriteLine("After =================");




            arr[0, 3] = 300;
            for (int i = 0; i < arr.GetLength(0);i++)
            {
                for (int j = 0; j < arr.GetLength(1); j ++)
                {
                    Console.Write("([{0} {1}], {2}", i, j, arr[i, j]);
                }
                Console.WriteLine("");

            }


        }
    }
}

 

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] arr = new int[4, 2]
            {
               {300,301},
               {100,302},
               {100,100},
               {101,101},
            };

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

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

            arr[0, 0] = 100;
            Console.WriteLine("AFTER======================================");

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


        }
    }
}

================================================

 

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래 = 100~
            //풀 = 200~
            //물 = 300~
            Console.WriteLine("App");

            int[,] arr = new int[3, 3]
            {
               {100,100,101 },
               {100,100,100 },
               {100,100,100 }
            };

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

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

            arr[0, 0] = 300;
            arr[0, 1] = 301;
            arr[0, 2] = 302;
            arr[1, 1] = 303;
            arr[1, 2] = 304;
            arr[2, 2] = 303;

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

        }
    }
}

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //모래, 풀 = 100~            
            //물 = 300~
            Console.WriteLine("App");

            int[,] arr = new int[3, 4]
            {
                {100,100,100,100 },
                {101,101,101,101 },
                {100,100,100,101 }
            };

            Console.WriteLine(arr.GetLength(0));
            Console.WriteLine(arr.GetLength(1));

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

        }
    }
}

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[5, 4]
            {
                {101,100,100,300 },
                {101,101,101,101 },
                {100,100,100,100 },
                {100,100,100,101 },
                {100,101,101,100 },
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

            int[,] unit = new int[5, 4]
            {
                {0,0,0,0 },
                {0,0,0,0 },
                {0,0,0,0 },
                {0,0,0,0 },
                {0,0,0,1 }
            };

            Console.WriteLine(unit.GetLength(0));
            Console.WriteLine(unit.GetLength(1));


            Console.WriteLine("MAP============================");

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

            Console.WriteLine("UNIT============================");

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

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[4, 4]
            {
                {100,101,101,101 },
                {100,100,301,302 },
                {100,303,304,305 },
                {100,100,100,100 }
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

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

 

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[3, 4]
            {
                {301,302,303,304 },
                {100,301,305,100 },
                {101,101,101,100 }
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

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

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[2, 4]
            {
                {0,0,0,0 },
                {0,0,0,0 }
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

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

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력


            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[3, 5]
            {
                {300,301,302,303,100},
                {100,304,305,306,308 },
                {100,100,304,307,100 }
            };

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

            map[0, 0] = 100;
            map[0, 1] = 100;
            map[0, 2] = 101;
            map[0, 3] = 101;
            map[0, 4] = 101;
            map[1, 1] = 100;
            map[1, 2] = 100;
            map[1, 3] = 309;
            map[1, 4] = 304;
            map[2, 2] = 310;
            map[2, 3] = 309;
            map[2, 4] = 307;

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

================================================

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

namespace Study004
{
    class App
    {
        //생성자
        public App()
        {
            //배열 선언 및 초기화

            //배열의 각 차원 길이 출력

            //배열의 요소 출력

            //캐릭터 위치 = 1
            //풀 = 100
            //모래 = 101            
            //물 = 300~
            Console.WriteLine("App");

            int[,] map = new int[8, 6];

            map[2, 2] = 7;
            map[2, 3] = 7;
            map[3, 2] = 8;
            map[3, 3] = 8;

            Console.WriteLine(map.GetLength(0));
            Console.WriteLine(map.GetLength(1));

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

'C# > 수업내용' 카테고리의 다른 글

C# 21.03.22.수업내용  (0) 2021.03.22
C# 21.03.19.수업내용  (0) 2021.03.19
C# 21.03.17.수업내용  (0) 2021.03.17
C#21.03.16.수업내용  (0) 2021.03.16
C#21.03.15.수업내용  (0) 2021.03.15