상세 컨텐츠

본문 제목

List

C#

by 메타 스토리 2023. 9. 5. 02:25

본문

List 생성 및 요소 추가:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<string> colors = new List<string>();
        
        // 요소 추가
        colors.Add("Red");    // "Red" 요소 추가
        colors.Add("Green");  // "Green" 요소 추가
        colors.Add("Blue");   // "Blue" 요소 추가

        // 출력
        foreach (string color in colors)
        {
            Console.WriteLine(color);
        }
    }
}
// 결과:
// Red
// Green
// Blue

 

List 요소 삭제:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<string> fruits = new List<string>();
        
        // 요소 추가
        fruits.Add("Apple");
        fruits.Add("Banana");
        fruits.Add("Orange");

        // "Banana" 요소 삭제
        fruits.Remove("Banana");

        // 출력
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}
// 결과:
// Apple
// Orange

 

List에서 요소 찾기:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // 요소 찾기
        int searchNumber = 3;
        bool containsNumber = numbers.Contains(searchNumber);

        Console.WriteLine($"List contains {searchNumber}: {containsNumber}");
    }
}
// 결과:
// List contains 3: True

 

List의 특정 위치에 요소 삽입:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<int> numbers = new List<int> { 1, 2, 4, 5 };

        // 특정 위치에 요소 삽입
        int insertIndex = 2;
        int insertValue = 3;
        numbers.Insert(insertIndex, insertValue);

        // 출력
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}
// 결과:
// 1
// 2
// 3
// 4
// 5

 

오름차순으로 List 정렬하기:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<int> numbers = new List<int> { 5, 2, 1, 4, 3 };

        // 오름차순 정렬
        numbers.Sort();

        // 출력
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}
// 결과:
// 1
// 2
// 3
// 4
// 5

 

내림차순으로 List 정렬하기:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<int> numbers = new List<int> { 5, 2, 1, 4, 3 };

        // 오름차순 정렬
        numbers.Sort();

        // 내림차순으로 뒤집기
        numbers.Reverse();

        // 출력
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}
// 결과:
// 5
// 4
// 3
// 2
// 1

 

 foreach문 

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List 생성
        List<string> fruits = new List<string> { "사과", "바나나", "체리", "딸기" };

        // List의 모든 요소에 대해 반복
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}
// 결과:
// 사과
// 바나나
// 체리
// 딸기

 

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

Stack  (0) 2023.09.05
Queue  (0) 2023.09.05
string.Format  (0) 2023.09.05
문자열  (0) 2023.09.05
Path  (0) 2023.09.04

관련글 더보기