Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में List में निर्दिष्ट इंडेक्स पर तत्व प्राप्त या सेट करें

List<T> इंडेक्सर (this[int index]) के माध्यम से शून्य-आधारित इंडेक्स द्वारा तत्वों तक त्वरित पहुंच प्रदान करता है। यह पढ़ने और लिखने दोनों की अनुमति देता है।

उदाहरण 1: तत्व पढ़ना

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(String[] args)
    {
        List<String> list = new List<String>();
        list.Add("100");
        list.Add("200");
        list.Add("300");
        list.Add("400");
        list.Add("500");
        list.Add("600");
        list.Add("700");
        list.Add("800");
        list.Add("900");
        list.Add("1000");
        Console.WriteLine("Element at index 0 = " + list[0]);
        Console.WriteLine("Element at index 1 = " + list[1]);
        Console.WriteLine("Element at index 2 = " + list[2]);
        Console.WriteLine("Element at index 3 = " + list[3]);
        Console.WriteLine("Element at index 4 = " + list[4]);
        Console.WriteLine("Element at index 5 = " + list[5]);
        Console.WriteLine("Element at index 6 = " + list[6]);
        Console.WriteLine("Element at index 7 = " + list[7]);
    }
}

आउटपुट

Element at index 0 = 100
Element at index 1 = 200
Element at index 2 = 300
Element at index 3 = 400
Element at index 4 = 500
Element at index 5 = 600
Element at index 6 = 700
Element at index 7 = 800

उदाहरण 2: तत्व पढ़ना और अपडेट करना

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(String[] args)
    {
        List<String> list = new List<String>();
        list.Add("100");
        list.Add("200");
        list.Add("300");
        list.Add("400");
        list.Add("500");
        list.Add("600");
        list.Add("700");
        list.Add("800");
        list.Add("900");
        list.Add("1000");
        Console.WriteLine("Element at index 0 = " + list[0]);
        Console.WriteLine("Element at index 1 = " + list[1]);
        Console.WriteLine("Element at index 2 = " + list[2]);
        Console.WriteLine("Element at index 3 = " + list[3]);
        Console.WriteLine("Element at index 4 = " + list[4]);
        Console.WriteLine("Element at index 5 = " + list[5]);
        Console.WriteLine("Element at index 6 = " + list[6]);
        Console.WriteLine("Element at index 7 = " + list[7]);
        list[3] = "450";
        Console.WriteLine("Element at index 3 [UPDATED] = " + list[3]);
    }
}

आउटपुट

Element at index 0 = 100
Element at index 1 = 200
Element at index 2 = 300
Element at index 3 = 400
Element at index 4 = 500
Element at index 5 = 600
Element at index 6 = 700
Element at index 7 = 800
Element at index 3 [UPDATED] = 450

इंडेक्सर O(1) एक्सेस प्रदान करता है। अमान्य इंडेक्स (नकारात्मक या >= Count) ArgumentOutOfRangeException फेंकता है। मान प्रकारों के लिए, इंडेक्सर मान की एक प्रति लौटाता है; संदर्भ प्रकारों के लिए, यह संदर्भ लौटाता है।

  1. - Python में लिस्ट का सेकंड-टू-लास्ट एलिमेंट कैसे प्राप्त करें?

    Python सीक्वेंस, जिसमें लिस्ट ऑब्जेक्ट शामिल है, इंडेक्सिंग की अनुमति देता है। लिस्ट में किसी भी एलिमेंट को जीरो-बेस्ड इंडेक्स का उपयोग करके एक्सेस किया जा सकता है। यदि इंडेक्स नेगेटिव नंबर है, तो इंडेक्स की गिनती अंत से शुरू होती है। चूंकि हम लिस्ट में सेकंड-टू-लास्ट एलिमेंट चाहते हैं, -2 को इंडेक्

  1. - Python में लिस्ट का लास्ट एलिमेंट कैसे प्राप्त करें?

    Python सीक्वेंस, जिसमें लिस्ट ऑब्जेक्ट शामिल है, इंडेक्सिंग की अनुमति देता है। लिस्ट में किसी भी एलिमेंट को जीरो-बेस्ड इंडेक्स का उपयोग करके एक्सेस किया जा सकता है। यदि इंडेक्स नेगेटिव नंबर है, तो इंडेक्स की गिनती अंत से शुरू होती है। चूंकि हम लिस्ट में लास्ट एलिमेंट चाहते हैं, -1 को इंडेक्स के रूप

  1. - पायथन में सूची में किसी तत्व का इंडेक्स कैसे खोजें?

    index() मेथड जो सूची (साथ ही स्ट्रिंग और टुपल जैसे अन्य सीक्वेंस टाइप) के लिए उपलब्ध है, किसी विशेष तत्व की पहली ऑकरेंस खोजने के लिए उपयोगी है। >>> L1 = [a, b, c, a, x] >>> L1 [a, b, c, a, x] >>> L1.index(a) 0 एक तत्व के सभी ऑकरेंस के इंडेक्स प्राप्त करने के लिए, enumera