==========================================================================================
C#
==========================================================================================
<C#>
=12장 까지 배운 후 구현 가능(프로젝트 진행)
=컬렉션
-데이터를 처리하기 위한 개념
= ArrayList - 사용자의 요구에 따라 크기가 동적으로 증가하는 배열을 사용하도록 IList 인터페이스를 구현합니다
: object 형식 객체를 담을수 있음 => BOXING / UNBOXING = 오버헤드(성능 저하)
=> 일반화 컬렉션(Generic Collection)에서 해결 될 것임
- Add() : 뒤에 추가
using System; using System.Collections;//.Generic;
namespace UsingList { class Program { static void Main(string[] args) { ArrayList list = new ArrayList(); for (int i = 0; i < 5; ++i) list.Add(i);
foreach (object obj in list) Console.Write("{0} ", obj); Console.WriteLine();
list.RemoveAt(2);
foreach (object obj in list) Console.Write("{0} ", obj); Console.WriteLine();
list.Insert(2, 2);
foreach (object obj in list) Console.Write("{0} ", obj); Console.WriteLine();
list.Add("abc"); list.Add("def");
for(int i=0; i < list.Count; ++i) Console.Write("{0} ", list[i]); Console.WriteLine();
} } }
|


=Queue : FIFO
using System; using System.Collections;//.Generic;
namespace UsingQueue { class Program { static void Main(string[] args) { Queue que = new Queue(); que.Enqueue(1); que.Enqueue(2); que.Enqueue(3); que.Enqueue(4); que.Enqueue(5);
while (que.Count > 0) Console.WriteLine(que.Dequeue()); } } }
|


=Stack : LIFO
using System; using System.Collections;
namespace UsingStack { class Program { static void Main(string[] args) { Stack stack = new Stack();
stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); stack.Push(5);
while (stack.Count > 0) Console.WriteLine(stack.Pop()); } } }
|


=Hashtable : 키(KEY - INT / FLOAT / CLASS ), 값(VALUE)로 이루어진 데이터
: 키를 해싱을 통해 테이블내의 주소를 계산(DIRECT ACCESS - 순차 검색이 아님 => 속도 빠름)
: 다루기 간편 / 탐색속도 빠름
(단점 : 용량을 많이 차지함)
using System; using System.Collections;
namespace UsingHashtable { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht["하나"] = "one"; // [KEY] VALUE ht["둘"] = "tow"; ht["셋"] = "three"; ht["넨"] = "four"; ht["다섯"] = "five";
Console.WriteLine(ht["하나"]); Console.WriteLine(ht["둘"]); Console.WriteLine(ht["셋"]); Console.WriteLine(ht["넨"]); Console.WriteLine(ht["다섯"]);
} } }
|


=Indexer : 중요 - 인덱스를 이용해서 데이터에 접근 - 프로퍼티
using System;
class NameCard { private string[] names;
public NameCard(int i) { names = new string[i]; // 안에서는 names라는 배열임 - 외부에서는 모름 // 외부에서는 this(my)로 알고있음 }
public string this[int index] // index 이름 바꿔도 상관없음 // this Namecard의 인스턴스 { get // 프로퍼티 { if (names[index] == null) return "아무 값도 저장되어 있지 않습니다.."; else return names[index]; } set // 프로퍼티 { if (value == null) Console.WriteLine("null 값을 저장할 수 없습니다."); else names[index] = value; } } }
class MainApp { public static void Main() { const int max = 5; NameCard my = new NameCard(max); my[0] = "박상현"; // my[1] 은 일부러 비워뒀습니다. my[2] = "송민규"; my[3] = "신유나"; my[4] = "김반석";
for (int i = 0; i < max; i++) Console.WriteLine(my[i]); } }
|


=FOREACH 사용 가능하도록 수정
:IEnumatator, IEnumerable 인터페이스 상속
-...ator | ...able => 같이 다님
- 인터페이스에서 반드시 작성해야할 메서드 표시됨


using System; using System.Collections;
namespace Indexer { class NameCard : IEnumerable, IEnumerator // 인터페이스 - FOREACH 사용 가능하도록 상속 받기 { // MoveNext() / Reset() / cURRENT{get;} // GetEnumerator - 구현하기 private string[] names; int position = -1; // Current()
public NameCard(int i) { names = new string[i]; // 안에서는 names라는 배열임 - 외부에서는 모름 // 외부에서는 this(my)로 알고있음 }
public string this[int index] // index 이름 바꿔도 상관없음 // this Namecard의 인스턴스 { get // 프로퍼티 { if (names[index] == null) return "아무 값도 저장되어 있지 않습니다.."; else return names[index]; } set // 프로퍼티 { if (value == null) Console.WriteLine("null 값을 저장할 수 없습니다."); else names[index] = value; } }
public object Current // IEnumerator 멤버 { get { return names[position]; } }
public bool MoveNext() // IEnumerator 멤버 { if (position == names.Length - 1) { Reset(); return false; } position++; return (position < names.Length); }
public void Reset() // IEnumerator 멤버 { position = -1; }
public IEnumerator GetEnumerator() // IEnumerable 멤버 { for (int i = 0; i < names.Length; i++) { yield return (names[i]); } } }
class MyList : IEnumerable, IEnumerator // 인터페이스 - FOREACH 사용 가능하도록 상속 받기 { // MoveNext() / Reset() / cURRENT{get;} // GetEnumerator - 구현하기 private int[] array; int position = -1; // Current()
public MyList() { array = new int[3]; }
public int this[int index] { get { return array[index]; }
set { if (index >= array.Length) { Array.Resize<int>(ref array, index + 1); Console.WriteLine("Array Resize : {0}", array.Length); } array[index] = value; } }
public int Length { get { return array.Length; } }
public object Current // IEnumerator 멤버 { get { return array[position]; } }
public bool MoveNext() // IEnumerator 멤버 { if(position == array.Length -1) { Reset(); return false; } position++; return (position < array.Length); }
public void Reset() // IEnumerator 멤버 { position = -1; }
public IEnumerator GetEnumerator() // IEnumerable 멤버 { for(int i = 0; i < array.Length; i++) { yield return (array[i]); } } }
class MainApp { public static void Main() { const int max = 5; NameCard my = new NameCard(max); MyList list = new MyList();
my[0] = "박상현"; //my[1] 은 일부러 비워뒀습니다. my[2] = "송민규"; my[3] = "신유나"; my[4] = "김반석";
for (int i = 0; i < max; i++) Console.WriteLine(my[i]);
Console.WriteLine("\nforeach\n");
foreach (string a in my) Console.WriteLine(a);
Console.WriteLine("\n");
for (int i = 0; i < 5; ++i) list[i] = i;
//for (int i = 0; i < list.Length; ++i) // Console.WriteLine(list[i]);
Console.WriteLine("\nforeach\n");
foreach (int e in list) Console.WriteLine(e); } } }
|


=학생 객체 (콜렉션 중에서) 세 개필드(수학,영어,과학) 두 개(총점, 평균) 메서드 => 순위별로 SORT
이름입력 하기싫으면 q:
수학
영어
과학
이름 수학 영어 과학 총점 평균 순위