Table of Contents:
Computer Programming Arrays – Definition, Examples & Types (C, Java, Python)
An array is a data structure that allows you to store multiple values of the same data type in a single variable. Instead of creating separate variables for each value, arrays help organize data efficiently.
Example:
Instead of:
int n1 = 10;
int n2 = 20;
int n3 = 30;
You can use:
int numbers[3] = {10, 20, 30};
What Is an Array?
An array is a collection of elements stored in contiguous memory locations, meaning values are placed one after another in memory.
Each element is accessed using an index, starting from 0 in most programming languages.
Key Features
- Stores multiple values of the same type
- Uses indices to access elements
- Fixed size in languages like C and Java
- Dynamic size in Python (list)
Arrays in Different Programming Languages
programming arrays, what is an array, array examples, 1D array, 2D array, multidimensional array, C arrays, Java arrays, Python list arrays, array operations, programming basics
Arrays in C
C uses the following syntax to declare an array:
Declaration
int marks[5];
Initialization
int marks[5] = {50, 60, 70, 80, 90};
Accessing Elements
printf("%d", marks[2]); // Output: 70
Arrays in Java
Java treats arrays as objects.
Declaration
int[] marks = new int[5];
Initialization
int[] marks = {50, 60, 70, 80, 90};
Accessing Elements
System.out.println(marks[2]); // Output: 70
Arrays in Python
Python does not have traditional arrays. Instead, it uses lists, which are more flexible.
Declaration & Initialization
marks = [50, 60, 70, 80, 90]
Accessing Elements
print(marks[2]) # Output: 70
Types of Arrays
1. One-Dimensional Array
A simple linear list of elements.
Example in C:
int nums[4] = {1, 2, 3, 4};
2. Multi-Dimensional Arrays
These are arrays within arrays (matrix-like structures).
Example: 2D Array in C
int matrix[2][2] = {
{1, 2},
{3, 4}
};
Example: Python List of Lists
matrix = [
[1, 2],
[3, 4]
]
Common Array Operations
Here are some typical operations performed on arrays:
1. Traversing
Access each element using a loop.
Example in Java:
for(int i=0; i<marks.length; i++){
System.out.println(marks[i]);
}
2. Updating
marks[1] = 95;
3. Searching
Iterate to find a value.
Memory Representation
Arrays store their values in continuous memory blocks, which makes them fast for indexing but inflexible when changing size (in languages like C and Java).
Python lists, however, are dynamic and resize automatically.
Uses of Arrays
Arrays are used in:
- Storing student marks
- Managing lists of items
- Matrix operations
- Image and graphics processing
- Sorting and searching algorithms
- Storing string collections
Arrays are fundamental to data structures and algorithms.
FAQ
What is an array in programming?
An array is a data structure that stores multiple values of the same type using indices.
What is the index of the first element in an array?
In most languages like C, Java, and Python, array indexing starts at 0.
Can Python store arrays?
Python uses lists, which act like dynamic arrays.
What are multidimensional arrays?
Arrays of arrays, such as tables or matrices.
Can arrays store different data types?
Most languages require arrays to hold the same data type.
Python lists can hold mixed data types.