General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Datatypes
  3. Composite Datatypes

Arrays

Arrays in computer programming are a fundamental data structure used to store collections of elements. Each element in an array is identified by at least one array index or key. An array can store data of a specific type, be it integer, string, or any other data types. The key feature of an array is that it allows random access of elements. This means that one can access any element in the array directly if the index of the element is known.

Arrays are useful in situations where there is a need to store a list of values of the same type. They are often used to store data like lists of usernames, scores in a game, or inventory items in an application.

Example in Python

In Python, arrays can be created by using the list data type, which is more flexible and can contain elements of different data types.

# Creating an array in Python
numbers = [1, 2, 3, 4, 5]  # This is a list, commonly used as an array in Python
print(numbers[2])  # Accessing the third element (index starts at 0), which will print 3

Example in C#

In C#, arrays are strongly typed, meaning they can only contain elements of the same data type. They are defined with a specific data type, and their size must be specified at the time of declaration (or initialized directly).

// Creating an array in C#
int[] numbers = new int[5] {1, 2, 3, 4, 5};  // Declaration and initialization of an array
Console.WriteLine(numbers[2]);  // Accessing the third element, which will print 3
PreviousComposite DatatypesNextStrings

Last updated 1 year ago

Was this helpful?