Array - potatoscript/csharp GitHub Wiki

πŸ₯” Arrays in C# πŸ₯”

πŸ₯” What is an Array? πŸ₯”

An array is like a box that can hold multiple items, all of the same type. Instead of having separate variables for each item (like item1, item2, item3, etc.), you can store all the items in one single array.

Imagine you have a box with several compartments. Each compartment can hold one item, and you can easily access any item by referring to its position, or "index" in the box.

In C#, arrays are used to store multiple values in a single variable.

πŸ₯” Why Use Arrays? πŸ₯”

  • Simplified Code: Instead of creating multiple variables, you can use one array to hold a collection of items.
  • Efficient: Arrays help organize related data in a neat and manageable way.
  • Easy Access: You can quickly access any item in an array using its index number.

πŸ₯” Creating an Array πŸ₯”

To create an array in C#, you need to specify:

  1. The type of the data the array will hold (like int, string, etc.)
  2. The size of the array, or how many items it will hold.

Here’s how to create an array:

// Create an array of integers
int[] numbers = new int[5];

In this example, we created an array called numbers that can hold 5 integers.

πŸ₯” Accessing Array Elements πŸ₯”

Each item in an array is located at a specific position, known as its index. In C#, the index starts at 0. So, the first item is at index 0, the second at index 1, and so on.

Here’s how you can access an element in an array:

// Assigning values to the array
numbers[0] = 10;  // First element
numbers[1] = 20;  // Second element
numbers[2] = 30;  // Third element
numbers[3] = 40;  // Fourth element
numbers[4] = 50;  // Fifth element

// Accessing array elements
Console.WriteLine(numbers[0]);  // Output: 10
Console.WriteLine(numbers[1]);  // Output: 20
Console.WriteLine(numbers[2]);  // Output: 30
Console.WriteLine(numbers[3]);  // Output: 40
Console.WriteLine(numbers[4]);  // Output: 50

πŸ₯” Array Initialization πŸ₯”

You can also initialize an array with values when you create it, so you don’t have to assign values later.

// Create and initialize an array with values
int[] numbers = { 10, 20, 30, 40, 50 };

// Accessing array elements
Console.WriteLine(numbers[0]);  // Output: 10
Console.WriteLine(numbers[1]);  // Output: 20

πŸ₯” Iterating Over an Array with a Loop πŸ₯”

One of the most common tasks with arrays is to loop through all the elements and do something with each one. You can do this with a for loop or a foreach loop.

πŸ₯” Using a For Loop πŸ₯”

A for loop lets you go through each element of the array by using the index.

// Create and initialize an array
int[] numbers = { 10, 20, 30, 40, 50 };

// Using a for loop to go through the array
for (int i = 0; i < numbers.Length; i++)
{
    Console.WriteLine(numbers[i]);  // Output: 10, 20, 30, 40, 50
}
  • numbers.Length gives the size of the array (how many items it contains).
  • i represents the index of each element.

πŸ₯” Using a Foreach Loop πŸ₯”

A foreach loop is even simpler and automatically goes through each item in the array, without needing to know the index.

// Using a foreach loop to go through the array
foreach (int number in numbers)
{
    Console.WriteLine(number);  // Output: 10, 20, 30, 40, 50
}

πŸ₯” Multi-Dimensional Arrays πŸ₯”

Arrays in C# can have more than one dimension. This means that you can have an array of arrays. For example, if you want to create a 2D grid of numbers (like a table), you can use a 2D array.

// Create a 2D array (like a table)
int[,] grid = new int[3, 3]  // 3 rows, 3 columns
{
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 }
};

// Accessing elements in a 2D array
Console.WriteLine(grid[0, 0]);  // Output: 1
Console.WriteLine(grid[1, 1]);  // Output: 5
Console.WriteLine(grid[2, 2]);  // Output: 9

πŸ₯” Jagged Arrays πŸ₯”

A jagged array is an array of arrays, but each inner array can have a different length. This is like having a list of lists, where each list can have different sizes.

// Create a jagged array
int[][] jaggedArray = new int[3][];

// Initialize the inner arrays
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };

// Accessing elements in a jagged array
Console.WriteLine(jaggedArray[0][0]);  // Output: 1
Console.WriteLine(jaggedArray[1][1]);  // Output: 5
Console.WriteLine(jaggedArray[2][3]);  // Output: 9

πŸ₯” Array Methods in C# πŸ₯”

There are many built-in methods in C# that can help you work with arrays. Some of the common ones are:

  • Array.Length: Returns the number of elements in an array.
  • Array.Sort(): Sorts the elements in an array.
  • Array.Reverse(): Reverses the order of elements in an array.
  • Array.IndexOf(): Finds the index of a particular element.
int[] numbers = { 30, 10, 50, 40, 20 };

// Sorting the array
Array.Sort(numbers);
Console.WriteLine("Sorted Array: ");
foreach (int number in numbers)
{
    Console.WriteLine(number);  // Output: 10, 20, 30, 40, 50
}

// Reversing the array
Array.Reverse(numbers);
Console.WriteLine("Reversed Array: ");
foreach (int number in numbers)
{
    Console.WriteLine(number);  // Output: 50, 40, 30, 20, 10
}

// Finding the index of a number
int index = Array.IndexOf(numbers, 40);
Console.WriteLine("Index of 40: " + index);  // Output: 1

πŸ₯” When to Use Arrays? πŸ₯”

You should use arrays when:

  • You have a collection of similar items (like a list of test scores or product prices).
  • You need to store a fixed number of items and access them quickly by index.
  • You want to loop through the collection of items efficiently.

πŸ₯” Summary of Arrays in C# πŸ₯”

  • An array is a collection of items, all of the same type, stored in a single variable.
  • You can create an array with a specific size and access its elements using indices (starting from 0).
  • Arrays can be multi-dimensional (like a table) or jagged (arrays of arrays with different sizes).
  • C# provides many methods like Sort(), Reverse(), and IndexOf() to work with arrays.
  • Arrays are useful when you have multiple similar items and need to access or manipulate them efficiently.

  string s = "1,4,5,6,7";
  int[] nums = Array.ConvertAll(s.Split(','), int.Parse);
  int[] nums = s.Split(',').Selected(int.Parse).ToArray();

home

Two-Dimensional-Array

   public List<List<string>> data { get; set;}
   data = new List<List<string>>();
   for(var k=0;k<10;k++)
   {
      data.Add(new List<string>());
      for(var i=0;i<10;i++)
      {
         data[k].Add(k);
      }
   }
⚠️ **GitHub.com Fallback** ⚠️