Tutorials Home   >   Programming Basics   >   Arrays and Lists in Programming

Arrays and Lists in Programming

What are Arrays and Lists?

An array or list is a collection of items stored in a single variable. These items are usually of the same type (in arrays) but can be of mixed types in lists (in languages like Python).

Think of an array/list as a row of boxes, each holding one piece of data. Each box has a position called an index, usually starting from 0.

Example: Storing the marks of 5 students:

marks = [80, 75, 90, 85, 70]
  • marks[0] → 80

  • marks[4] → 70

Arrays and lists make it easy to store, access, and process multiple values efficiently.


Creating Arrays / Lists

Python (Lists)

# List of numbers
numbers = [10, 20, 30, 40]
# List of strings
fruits = [“apple”, “banana”, “cherry”]# Mixed data types
mixed = [10, “apple”, True]

Java (Arrays)

// Array of integers
int[] numbers = {10, 20, 30, 40};
// Array of strings
String[] fruits = {“apple”, “banana”, “cherry”};

Java (ArrayList) – Dynamic lists

import java.util.ArrayList;

ArrayList<String> fruits = new ArrayList<>();
fruits.add(“apple”);
fruits.add(“banana”);
fruits.add(“cherry”);


Accessing Elements

You can access individual items using their index:

numbers = [10, 20, 30, 40]
print(numbers[0]) # 10
print(numbers[3]) # 40

Java:

int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]); // 10
System.out.println(numbers[3]); // 40

Note: Indexing starts at 0, not 1.


Updating Elements

You can change a value at a specific index.

Python:

numbers = [10, 20, 30, 40]
numbers[1] = 25
print(numbers) # [10, 25, 30, 40]

Java:

int[] numbers = {10, 20, 30, 40};
numbers[1] = 25;
System.out.println(numbers[1]); // 25

Adding and Removing Elements

Python (Lists)

fruits = ["apple", "banana"]

# Adding elements
fruits.append(“cherry”) # Adds at end
fruits.insert(1, “orange”) # Inserts at index 1

# Removing elements
fruits.remove(“banana”) # Removes by value
fruits.pop() # Removes last element
print(fruits)

Java (ArrayList)

import java.util.ArrayList;
ArrayList<String> fruits = new ArrayList<>();
fruits.add(“apple”); // Add at end
fruits.add(1, “orange”); // Add at index 1
fruits.remove(“apple”); // Remove by value
fruits.remove(1); // Remove by index

Looping Through Arrays / Lists

You can process all items using loops.

Python Example:

numbers = [10, 20, 30, 40]
for num in numbers:
print(num)

Java Example (Array):

int[] numbers = {10, 20, 30, 40};
for(int num : numbers){
System.out.println(num);
}

Java Example (ArrayList):

ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
for(String fruit : fruits){
System.out.println(fruit);
}

Common Operations on Arrays / Lists

Operation Python Example Java Example (ArrayList)
Length / Size len(numbers) fruits.size()
Check element in list "apple" in fruits fruits.contains("apple")
Index of element fruits.index("banana") fruits.indexOf("banana")
Sort numbers.sort() Collections.sort(fruits)
Reverse numbers.reverse() Collections.reverse(fruits)

Multi-Dimensional Arrays / Lists

You can have lists or arrays inside lists, useful for tables, grids, or matrices.

Python Example (2D list):

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][1]) # 2

Java Example (2D array):

int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(matrix[0][1]); // 2

Best Practices for Arrays / Lists

  1. Use descriptive names: marks instead of a.

  2. Use loops to process elements efficiently.

  3. Avoid out-of-bounds errors by checking index limits.

  4. Choose the right type: array for fixed size, list/ArrayList for dynamic size.

  5. Use built-in functions for sorting, searching, or reversing for simplicity.


Summary

Arrays and lists are fundamental data structures in programming:

  • Store multiple items in a single variable.

  • Access items using index starting from 0.

  • Update, add, or remove items as needed.

  • Use loops to process elements efficiently.

  • Support multi-dimensional storage for grids and tables.

  • Arrays have fixed size (Java), lists are dynamic (Python, ArrayList in Java).

Mastering arrays and lists is essential because they allow programs to handle large amounts of data efficiently, making them a cornerstone of programming.