gnu

블로그 이미지
by 군우

TAG CLOUD

  • Total hit
  • Today hit
  • Yesterday hit

'C#'에 해당되는 글 88건

  1. 2018.05.24
    [C#] Hashtable 클래스
  2. 2018.05.20
    [C#] Length와 Rank 속성
  3. 2018.05.20
    [C#] Clone(), Array.Copy배열 복제
  4. 2018.05.18
    [C#] 가변배열
  5. 2018.05.17
    [C#] 문자열 함수2
  6. 2018.05.17
    [C#] 문자열 비교함수
  7. 2018.05.17
    [C#] 문자열 함수
  8. 2018.05.17
    [c#] 배열 copy
  9. 2018.05.17
    [C#] 배열 copy함수
  10. 2018.05.17
    [C#] 배열 함수
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication13
{
    class SamplesHashtable
    {
        static void Main(string[] args)
        {
            Hashtable myHT = new Hashtable();
            myHT.Add("First","Hello");
            myHT.Add("Second""Would");
            myHT.Add("Third""welcome");
            myHT.Add("Four""!");
            Print("1.myHT 전체목록:", myHT);
            bool IsContain = myHT.ContainsKey("First");
            Console.Write("2.First 키의 존재여부:" + IsContain);
            Console.Write("3.키 값들만 출력 \n\t");
                ICollection mykey = myHT.Keys;
            foreach(object obj in mykey)
                Console.Write("{0}",obj);
            Console.WriteLine();
            Console.Write("4. Value들만 출력\n\t");
            ICollection myval = myHT.Values;
            foreach (object obj in myval)
                Console.Write("{0}", obj);
            Console.WriteLine();
            Console.Write("5. 데이터 삭제\n\t");
            Console.WriteLine("6.KEY First value {0}", myHT["First"]);
            Console.WriteLine("7.KEY Second value {0}", myHT["Second"]);
 
        }
        public static void Print(String info, Hashtable myList){
            IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
            Console.Write(info +"\n\t");
            while (myEnumerator.MoveNext())
            {
                Console.Write("{0}:{1},", myEnumerator.Key, myEnumerator.Value);
            }
            Console.Write("요소의 갯수:"+ myList.Count+ "\n");
        }
    }
    
}
 
cs


'C# > C# Concept' 카테고리의 다른 글

[C#] NameValueCollection 클래스  (0) 2018.05.24
[C#] Stack  (0) 2018.05.24
[C#] Length와 Rank 속성  (0) 2018.05.20
[C#] Clone(), Array.Copy배열 복제  (0) 2018.05.20
[C#] 가변배열  (0) 2018.05.18
AND
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
배열의 Length와 Rank 속성을 테스트하는 예제
**/
using System;
class ArrayDemo
{
    public static void Main()
    {
        int[] sample1 = { 3745213234 };
        int[,] sample2 = { { 344 }, { 7669 }, { 7754 } };
 
        int[][] sample3 = new int[3][];
        //하위 배열 생성
        sample3[0= new int[] { 34523 };
        sample3[1= new int[] { 68 };
        sample3[2= new int[] { 356 };
 
        Console.WriteLine("sample1의 길이=" + sample1.Length);
        Console.WriteLine("sample2의 길이=" + sample2.Length); //6
        Console.WriteLine("sample3의 길이=" + sample3.Length); //3
        Console.WriteLine();
 
        for (int i = 0; i < sample3.Length; i++)
        {
            Console.WriteLine("sample3[{0}]의 길이= {1}", i, sample3[i].Length);
        }
        Console.WriteLine();
        Console.WriteLine("sample1의 차원=" + sample1.Rank);
        Console.WriteLine("sample2의 차원=" + sample2.Rank);
        Console.WriteLine("sample3의 차원=" + sample3.Rank);//배열의 배열들만 묶으면 1차원
    }//main
}//class
 /***
 sample1의 길이=6
 sample2의 길이=6
 sample3의 길이=3
 sample3[0]의 길이= 4
 sample3[1]의 길이= 2
 sample3[2]의 길이= 3
 sample1의 차원=1
 sample2의 차원=2
 sample3의 차원=1
 ***/
 
cs


int [][] a; 이거는 배열의 배열들을 엮은 형태라서 rank로 차원을 구하면 1이나옴

c#에서 다차원 배열은 [ , ]  이거다. 

'C# > C# Concept' 카테고리의 다른 글

[C#] Stack  (0) 2018.05.24
[C#] Hashtable 클래스  (0) 2018.05.24
[C#] Clone(), Array.Copy배열 복제  (0) 2018.05.20
[C#] 가변배열  (0) 2018.05.18
[C#] 문자열 함수2  (0) 2018.05.17
AND
/**
Clone() 함수를 사용해서 배열을 복사하는 예제
**/
using System;
public class ArrayCloneTest
{
public static void Main()
{
int[] myOriginal = new int[] { 1, 2, 3, 4 };
int[] copy = (int[])myOriginal.Clone();// 명시적 변환을 해주어야함.
for (int i = 0; i < copy.Length; i++)
{
Console.WriteLine("copy[" + i + "]: " + copy[i]);
}
}//main
}
//class
/***
myCopy[0]:1 myCopy[1]:2 myCopy[2]:3 myCopy[3]:4
***/



Array.Copy(a,1,c,2,3); 을 이용한  예제 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
Copy() 함수를 이용한 부분배열 복사 I
**/
using System;
public class ArrayCopyTest
{
    static void Main()
    {
        int[] myOriginal = new int[] { -1-3-5-7-9 };
        int[] myCopy = { 246810 };
        Array.Copy(myOriginal, 1, myCopy, 22);
  //copy에 2번째 자리부터 3개로  m 의 1번째부터 넣는다.
        for (int i = 0; i < myCopy.Length; i++)
            Console.Write("myCopy[" + i + "]=" + myCopy[i] + '\t');
    }//main
}//class
 /*** 
  *Array.Copy(myOriginal, 0, myCopy, 2, 3); 2 4 -1 -3 -5
  *Array.Copy(myOriginal, 1, myCopy, 2, 2); =>  2 4 , -3 -5 -7 
 ***/
 
cs


'C# > C# Concept' 카테고리의 다른 글

[C#] Hashtable 클래스  (0) 2018.05.24
[C#] Length와 Rank 속성  (0) 2018.05.20
[C#] 가변배열  (0) 2018.05.18
[C#] 문자열 함수2  (0) 2018.05.17
[C#] Upcasting ,virtual, override  (0) 2018.05.03
AND

C#에는 가변 배열이라는 것이 존재하는데, 이 가변 배열은 다차원 배열과는 다른 개념이다.

배열의 배열정도로 생각하면 될 것 같다. 먼저 소스 코드를 보자. 



1
2
3
4
5
6
7
8
9
10
11
12
13
string[,] str = new string[32]
// 2차원 배열
     { "aa""bb" },
     { "cc""dd" },
     { "ee""ff" }
};
 
string[][] jagged_str = new string[3][] 
// 가변 배열(jagged array)
    new string[3] { "abc""def""ghi" },
     new string[2] { "qqq""www" },
     new string[] { "EAX""EDX""ECX""EDX""EDI""ESI" }
};
cs


위의 str은 2차원 배열을 선언한 것이고, 아래의 jagged_str은 2차원 가변 배열을 선언한 것이다.

보면 대략적인 차이를 알 수 있을 것이다.



선언시 자료형 뒤에 [] 문자 안에 차원 수 만큼 콤마(,)를 삽입하여 공간을 나누면 다차원 배열이고,

자료형 뒤에 [] 문자를 차원 수 만큼 적어주면 그것은 가변 배열이다.



주제가 가변 배열인 만큼, 다차원 배열에 대한 이야기는 그만하고 가변 배열에 대하여 이야기 하겠다.


위 예제의 가변 배열을 보면 생성자 호출 부분이 new string[3][] 인데,

이 처럼 가변 배열 선언 시 가장 뒤 쪽 괄호는 비워주어야 한다. 가변 배열은 위에서 말했듯이 배열의 배열같은 개념이라

string[3][]을 해석하자면 string 배열을 담고있는 배열인데, 그 원소가 3개 있는 것이다.



jagged_str[0]는 원소로 "abc", "def", "ghi"을 갖는 string 배열을 => string[3],

jagged_str[1]는 원소로 "qqq", "www"을 갖는 string 배열을 => string[2],

jagged_str[2]는 원소로 "EAX", "EDX", "ECS", "EDX", "EDI", "ESI"을 갖는 string 배열을 담고 있다. => string[6]

배열에서 원소로 갖는 배열들의 크기가 다 다를수 있기 때문에 가변 배열이라고 부르는 것이다.



그림으로 표현해보면 이렇다.

 



사용은 아래와 같이 할 수 있다.

1
2
3
4
5
6
7
8
foreach (string[] jagged in jagged_str)
{// 2차원 가변 배열의 각 원소를 뽑는다.
foreach (string str_element in jagged)
      {// 뽑아낸 원소(담고있는 배열)에서 각 원소를 뽑는다.
      Console.Write(str_element + " ");
      }
Console.WriteLine();
}
cs

 


결과는 다음과 같다.


'C# > C# Concept' 카테고리의 다른 글

[C#] Length와 Rank 속성  (0) 2018.05.20
[C#] Clone(), Array.Copy배열 복제  (0) 2018.05.20
[C#] 문자열 함수2  (0) 2018.05.17
[C#] Upcasting ,virtual, override  (0) 2018.05.03
[C#] BOXING, UNBOXING  (0) 2018.05.03
AND
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections;
 
namespace ConsoleApplication12 
{
    class SamplesArrayList
    {
        static void Main(string[] args)
        {
            ArrayList myAL = new ArrayList();
 
            myAL.Add("hi"); myAL.Add("welcome"); myAL.Add("to");
            myAL.Add("c#"); myAL.Add("wourld");
            Print("1.데이터삽입:", myAL);
 
            Queue myQueue = new Queue();
            myQueue.Enqueue("QueueData1");
            myQueue.Enqueue("QueueData2");
            myAL.AddRange(myQueue);
            Print("2. Queue에 내용을 arraylist에 삽입: ", myAL);
 
 
            myAL.Insert(2"novel");
            Print("3. index위치 2에 데이터 삽입:", myAL);
 
            myAL.Remove("hi");
            Print("4. hi 데이터 삭제 :", myAL);
 
            myAL.RemoveAt(2);
            Print("5. index위치 2의 데이터 삭제", myAL);
 
            myAL.RemoveRange(22);
            Print("6.index 위치 2로부터 2개의 데이터 삭제삭제 :", myAL);
 
            myAL.Sort(03null);
            Print("Sort(0, 3, null) .index 위치 2로부터 2개의 데이터 삭제삭제 :", myAL);
        }
 
        public static void Print(String info, IEnumerable myList)
        {
            IEnumerator myEnumerator = myList.GetEnumerator();
            Console.Write(info);
 
            while (myEnumerator.MoveNext())
                Console.Write(" {0},", myEnumerator.Current);
            Console.WriteLine();
        }
    }
}
cs


'C# > C# Concept' 카테고리의 다른 글

[C#] Clone(), Array.Copy배열 복제  (0) 2018.05.20
[C#] 가변배열  (0) 2018.05.18
[C#] Upcasting ,virtual, override  (0) 2018.05.03
[C#] BOXING, UNBOXING  (0) 2018.05.03
[C#] override new  (0) 2018.05.03
AND

using System;


namespace ConsoleApplication10

{

    class StringTest3

    {

        static void Main(string[] args)

        {

            String str1 = "hello jabook";

            string str2 = "jabook";

            str1 = str1.Substring(6);

            Console.WriteLine(str1);

            Console.WriteLine("str1.ReferenceEquals(str2) : {0}", object.ReferenceEquals(str1, str2));

            Console.WriteLine("str1.Equals(str2) : {0}", str1.Equals(str2));

            Console.WriteLine("str1 == str2 : {0} ", str1 == str2);

            Console.WriteLine("string.Equals(str1, str2): {0}", string.Equals(str1,str2));

        }

    }

}



'C# > C# Training' 카테고리의 다른 글

[C#] 문자열 함수  (0) 2018.05.17
[c#] 배열 copy  (0) 2018.05.17
[C#] 배열 copy함수  (0) 2018.05.17
[C#] 배열 함수  (0) 2018.05.17
[C#] 가변배열2  (0) 2018.05.17
AND
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication9
{
    class StringTest2
    {
        static void Main(string[] args)
        {
            string str = "Hello Wourld Hi Hello";
            Console.WriteLine("원문 str: {0}", str);
            Console.WriteLine();
            Console.WriteLine("Index of \"World\": {0}", str.IndexOf("World"));
            Console.WriteLine("LastIndexOf \"Hi\": {0}", str.LastIndexOf("World"));
            Console.WriteLine("Replace\"World\" to \"Tom\" :{0}", str.Replace("World","Tom"));
 
            Console.WriteLine("str의 substring(6): {0} ", str.Substring(6));
 
        }
    }
}
 
cs


'C# > C# Training' 카테고리의 다른 글

[C#] 문자열 비교함수  (0) 2018.05.17
[c#] 배열 copy  (0) 2018.05.17
[C#] 배열 copy함수  (0) 2018.05.17
[C#] 배열 함수  (0) 2018.05.17
[C#] 가변배열2  (0) 2018.05.17
AND
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
 
namespace ConsoleApplication7
{
    class ParamArray
    {
        public int[] CopyArray(int[] source)
        {
            int[] des = new int[source.Length];
 
            for (int i = 0; i < source.Length; i++)
                des[i] = source[i];
            return des;
        }
        public void CopyArray(int[] source, int[] target)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    target[i] = source[i];
                }
            }
        }
 
        class ParamArrayTest
        {
        static void Main(string[] args)
            {
 
                int[] arr = new int[] { 123 };
 
                int[] tar = new int[3];
 
                ParamArray p = new ParamArray();
 
                int[] result = p.CopyArray(arr);
 
                foreach (int r in result)
                {
                    Console.Write("result[" + r + "] =" + r + '\t');
                }
 
                Console.WriteLine();
 
                p.CopyArray(arr, tar);
 
                foreach (int t in tar)
                {
 
                    Console.Write("tar");
                }
            }
        }
    }
cs


'C# > C# Training' 카테고리의 다른 글

[C#] 문자열 비교함수  (0) 2018.05.17
[C#] 문자열 함수  (0) 2018.05.17
[C#] 배열 copy함수  (0) 2018.05.17
[C#] 배열 함수  (0) 2018.05.17
[C#] 가변배열2  (0) 2018.05.17
AND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApplication6

{

    class ArrayCopyTest

    {

        static void Main(string[] args)

        {

            int[] myOriginal = new int[] { -1, -3, -5, -7, -9 };

            int[] myCopy = { 2, 4, 6, 8, 10 };


            Array.Copy(myOriginal, 0, myCopy, 2, 3);

            // myOriginal의 0번째부터 카피하는데 ,myCopy , 2,3

            for (int i = 0; i<myCopy.Length;i++ )

            {

                Console.Write("myCopy[" + i + "]=" + myCopy[i] + '\t');

            }

        }

    }

}



'C# > C# Training' 카테고리의 다른 글

[C#] 문자열 함수  (0) 2018.05.17
[c#] 배열 copy  (0) 2018.05.17
[C#] 배열 함수  (0) 2018.05.17
[C#] 가변배열2  (0) 2018.05.17
[C#] 가변배열  (0) 2018.05.17
AND


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
32
33
34
35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication5
{
    class ArrayMethods
    {
        public static void Print(int[] arr)
        {
            foreach(int s in arr){
                Console.Write(s+ "\t");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int[] arr = {105,44,551000140355};
            Print(arr);
            Array.Sort(arr); // 오름차순 정렬
            Print(arr);
            Array.Reverse(arr);// 배열 역순 정렬
            Print(arr);
 
            int index55 = Array.IndexOf(arr,55);  //55가 처음  나오는 위치
            Console.WriteLine(index55); 
            int lastindex55 = Array.LastIndexOf(arr,55);
            // 마지막부터 검색하여 처음 나오는 위치
            Print(arr);
        }
    }
}
 
cs








출력화면


10      5       44      55      1000    140     3       55

3       5       10      44      55      55      140     1000

1000    140     55      55      44      10      5       3

2

1000    140     55      55      44      10      5       3

계속하려면 아무 키나 누르십시오 . . .

'C# > C# Training' 카테고리의 다른 글

[c#] 배열 copy  (0) 2018.05.17
[C#] 배열 copy함수  (0) 2018.05.17
[C#] 가변배열2  (0) 2018.05.17
[C#] 가변배열  (0) 2018.05.17
[C#] 객체 배열 52p  (0) 2018.05.10
AND

ARTICLE CATEGORY

분류 전체보기 (197)
C (0)
HTML (7)
C# (88)
Python (27)
IT (0)
Android (2)
Java (65)
Study (5)
JavaScript (1)
JSP (2)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2025/05   »
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

ARCHIVE

LINK