What is an Array? (C# vs Python)



To keep a bunch of a datatype in a container = Array

Check out more lessons in this course:

Art used in this video in part made by freepik, & smashicons

SUBSCRIBE FOR MORE:
SUPPORT ON PATREON:

Please follow me on social networks:
twitter:
instagram:
reddit:…

, ,

7 thoughts on “What is an Array? (C# vs Python)

  1. The first element is usually 0 because it denotes the offset from the start of the array. So by accessing item 1, you are offsetting one value away from the start of the array – which is actually the 2nd element in the array.

    In languages that deal more with data science, such as R and MATLAB, the start of the array is usually 1.

    If you're wanting a more Zarglar explanation, when computers were still young, the memory of the program was largely unmanaged. Think of it like a big book with numbered pages. You can put whatever you want on whatever page. If you wanted to set up an array, you had to designate memory for it, so in a book you can say that pages 15-18 will have an array of length 4 on there. When you type the beginning of your array variable, you would be given the address (or page number) for the first element in the array – which is page 15. To access it you would type array[0], since you are going to the array address and then moving forward 0 items. If you wanted the third item you would go to array[2] which would take you to page 15, then flip forward two pages to page 17 which is the third element in your array.

    Now, the reason this is so helpful is if we change the size of our elements, so for example, we now have pictures on the pages, and each picture takes up 2 pages. Well, we just initialise the array to look at elements that are two pages long, so when it comes time to flip to the right element in the array, we know to skip 2 pages at a time, rather than one page at a time.

    This helps us because it means we don't need to think of WHERE we're storing things in memory anymore, and think more about the data as a managed list for us. In the olden days, they would have had to remember that they had to flip forward 2 pages for certain elements, and flip forward 1 page for others. That could lead to all sorts of sticky situations if you're not careful.

  2. This code will give you all of the parts of an array from the beginning to the end:

    int[] hp = new int[100];

    // Print the HP of all 100 players
    for (int i = 0; i < hp.Length; i++)

    {
    hp[i] = 100;

    Console.WriteLine($"{i}: {hp[i]}");

    }
    Console.ReadKey();

    This python code does pretty much the same thing:

    hp = [100]*100

    for i in range(hp.__len__()):
    _ print(f"{i}: {hp[i]}")_

Leave a Reply

Your email address will not be published. Required fields are marked *