Total Pageviews

Tuesday, August 30, 2022

सी.शार्प में ARRAYLIST

 सी.शार्प में ARRAYLIST

2.1 ARRAYLIST की मुख्य विशेषताएँ

Properties

Explanation

Example

Length

Returns the length of array. Returns integer value.

int i = arr1.Length;

Rank

Returns total number of items in all the dimension. Returns integer value.

int i = arr1.Rank;

IsFixedSize

Check whether array is fixed size or not. Returns Boolean value

bool i = arr.IsFixedSize;

IsReadOnly

Check whether array is ReadOnly or not. Returns Boolean value

bool k = arr1.IsReadOnly;

Most common functions of Array class

 

Function

Explanation

Example

Sort

Sort an array

Array.Sort(arr);

Clear

Clear an array by removing all the items

Array.Clear(arr, 0, 3);

GetLength

Returns the number of elements

arr.GetLength(0);

GetValue

Returns the value of specified items

arr.GetValue(2);

IndexOf

Returns the index position of value

Array.IndexOf(arr,45);

Copy

Copy array elements to another elements

Array.Copy(arr1,arr1,3);

 

2.2 ArrayList  में item जोड़ना

 

  Syntax : ArrayList.add(object)

  object : The Item to be add the ArrayList

  ArrayList arr;

  arr.Add("Item1");

 

2.3 ArrayList  में item insert करना

 

  Syntax : ArrayList.insert(index,object)

  index : The position of the item in an ArrayList

  object : The Item to be add the ArrayList

  ArrayList arr;

  arr.Insert(3, "Item3");

 

2.4 ArrayList  से item remove करना

  Syntax : ArrayList.Remove(object)

  object : The Item to be add the ArrayList

  arr.Remove("item2")

 

2.5 ArrayList  से specified position से item remove करना

 

  Syntax : ArrayList.RemoveAt(index)

  index : the position of an item to remove from an ArrayList

  ItemList.RemoveAt(2)

How to sort ArrayList ?

  Syntax : ArrayList.Sort()

 

2.6 Arraylist के साथ For loop का प्रयोग करना

The for-loop is popular and useful. When using for on an ArrayList, you will need to cast the element after using its index. The [i] part in the example below demonstrates how to use the indexer on the ArrayList.

Program that uses ArrayList and for: C#

using System;

using System.Collections;

class Program

{

    static void Main()

    {

            //

            // Create an ArrayList with three strings.

            //

            ArrayList list = new ArrayList();

            list.Add("man");

            list.Add("woman");

            list.Add("plant");

            //

            // Loop over ArrayList.

            //

            for (int i = 0; i < list.Count; i++)

            {

                string value = list[i] as string;

                Console.WriteLine(value);

            }

    }

}

 

2.7 Arraylist में GetRange मेथड

Another method you can use is the GetRange method. This will return a subset of the original ArrayList in a new ArrayList. This is ideal when you know a certain part of your ArrayList has a different purpose or behavior.

 

2.8 Arraylist  में SetRange:

The SetRange method on ArrayList is also useful when you need to replace a range.

 

However:

I have not found SetRange to be useful, as often you will just want to replace elements in a for-loop.

 

Program that uses GetRange: C#

 

using System;

using System.Collections;

 

class Program

{

    static void Main()

    {

            //

            // Create an ArrayList with 4 strings.

            //

            ArrayList list = new ArrayList();

            list.Add("fish");

            list.Add("amphibian");

            list.Add("bird");

            list.Add("plant");

            //

            // Get last two elements in ArrayList.

            //

            ArrayList range = list.GetRange(2, 2);

            //

            // Display the elements.

            //

            foreach (string value in range)

            {

                Console.WriteLine(value); // bird, plant

            }

    }

}

No comments:

Post a Comment