AICollection Help

Chapter 8 - Arrays, Slices, Hashes, Lists, and Maps

Programming often involves managing collections of data, and various data structures are available to suit different requirements. Among the most common are arrays, slices, hashes, lists, and maps. Each of these structures has distinct characteristics and use cases, making them essential tools for developers.

This article provides an in-depth overview of these data structures, covering what they are, how they function, and how you can use them effectively in Python, PHP, C++, Zig, and Go.

An Overview of Arrays, Slices, Hashes, Lists, and Maps

What Are Arrays?

An array is a fixed-size collection of elements, all of the same type, stored in contiguous memory. Arrays are efficient for accessing elements by index but lack flexibility in size and advanced features.

Exploring Slices as a View of Data

A slice is a dynamically-sized view of an array or a portion of an array. Slices allow resizing, appending, and manipulation without altering the underlying array.

The Concept of Hashes or Dictionaries

A hash is a collection of key-value pairs. Keys are unique and map to specific values, allowing for fast lookups.

Lists: Ordered and Dynamic Data Structures

A list is an ordered, dynamic collection of elements. Unlike arrays, lists allow resizing and can store elements of different types.

Maps: Optimized Key-Value Structures

A map is similar to a hash, associating keys with values. Maps focus on efficient storage and retrieval through hashing.

Implementing Arrays in Different Languages

Arrays in Python

Python doesn’t have traditional arrays; instead, lists are used. However, the array module can be used for low-level, fixed-type arrays:

import array arr = array.array('i', [1, 2, 3, 4]) # 'i' denotes integer type print(arr[2]) # Access the third element

Arrays in PHP

In PHP, arrays are highly flexible and can act as ordered lists or associative arrays.

$arr = [1, 2, 3, 4]; echo $arr[2]; // Outputs 3

Arrays in C++

C++ provides static arrays:

#include <iostream> using namespace std; int main() { int arr[4] = {1, 2, 3, 4}; cout << arr[2] << endl; // Outputs 3 return 0; }

Arrays in Zig

Zig supports fixed-size arrays:

const std = @import("std"); pub fn main() void { var arr: [4]i32 = [4]i32{1, 2, 3, 4}; std.debug.print("{}\n", .{arr[2]}); // Outputs 3 }

Arrays in Go

In Go, arrays have a fixed size determined at declaration:

package main import "fmt" func main() { var arr = [4]int{1, 2, 3, 4} fmt.Println(arr[2]) // Outputs 3 }

How Slices Work in Practice

Using Slices in Python

Slicing allows accessing parts of a list or array:

arr = [1, 2, 3, 4] sub_arr = arr[1:3] # Slice from index 1 to 3 (exclusive) print(sub_arr) # Outputs [2, 3]

PHP and Array Slicing

PHP doesn’t have slices, but array_slice provides similar functionality:

$arr = [1, 2, 3, 4]; $sub_arr = array_slice($arr, 1, 2); print_r($sub_arr); // Outputs [2, 3]

Slicing in C++

In C++, slicing typically involves creating a new container like std::vector:

#include <iostream> #include <vector> using namespace std; int main() { vector<int> arr = {1, 2, 3, 4}; vector<int> sub_arr(arr.begin() + 1, arr.begin() + 3); for (int x : sub_arr) cout << x << " "; // Outputs 2 3 return 0; }

Slices in Zig Explained

Zig directly supports slices:

const std = @import("std"); pub fn main() void { var arr: [4]i32 = [4]i32{1, 2, 3, 4}; var slice = arr[1..3]; // Create a slice std.debug.print("{}, {}\n", .{slice[0], slice[1]}); // Outputs 2, 3 }

Slices in Go

Go slices are dynamic, built on top of arrays:

package main import "fmt" func main() { arr := []int{1, 2, 3, 4} slice := arr[1:3] // Create a slice from index 1 to 3 (exclusive) fmt.Println(slice) // Outputs [2 3] }

Key-Value Storage with Hashes

Hashes and Dictionaries in Python

Python dictionaries:

hash_map = {'a': 1, 'b': 2} print(hash_map['a']) # Outputs 1

Hash-Like Arrays in PHP

PHP arrays with keys serve as hashes:

$hash_map = ['a' => 1, 'b' => 2]; echo $hash_map['a']; // Outputs 1

Hash Tables in C++

C++ provides std::unordered_map for hash-based structures:

#include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<string, int> hash_map = {{"a", 1}, {"b", 2}}; cout << hash_map["a"] << endl; // Outputs 1 return 0; }

Implementing Hash Maps in Zig

Zig supports hash maps through its standard library:

const std = @import("std"); pub fn main() void { var hash_map = std.AutoHashMap([]const u8, i32).init(std.heap.page_allocator); hash_map.put("a", 1); std.debug.print("{}\n", .{hash_map.get("a").?}); // Outputs 1 }

Hash Maps in Go

Go has a built-in map type for key-value pairs:

package main import "fmt" func main() { hashMap := map[string]int{"a": 1, "b": 2} fmt.Println(hashMap["a"]) // Outputs 1 }

Working with Lists Across Languages

Creating and Manipulating Lists in Python

Python lists are dynamic:

lst = [1, 2, 3] lst.append(4) # Add an element print(lst) # Outputs [1, 2, 3, 4]

PHP Lists Using Arrays

PHP arrays can act as lists:

$list = [1, 2, 3]; $list[] = 4; // Add an element print_r($list); // Outputs [1, 2, 3, 4]

Linked Lists in C++

C++ provides std::list for doubly linked lists:

#include <iostream> #include <list> using namespace std; int main() { list<int> lst = {1, 2, 3}; lst.push_back(4); for (int x : lst) cout << x << " "; // Outputs 1 2 3 4 return 0; }

Using Lists Dynamically in Zig

Zig does not have built-in lists but allows dynamic resizing with slices:

const std = @import("std"); pub fn main() void { var allocator = std.heap.page_allocator; var list = std.ArrayList(i32).init(allocator); list.append(1); list.append(2); list.append(3); list.append(4); for (list.toSlice()) |item| std.debug.print("{} ", .{item}); // Outputs 1 2 3 4 }

Dynamic Lists in Go

Go slices function as dynamic lists:

package main import "fmt" func main() { list := []int{1, 2, 3} list = append(list, 4) // Add an element fmt.Println(list) // Outputs [1 2 3 4] }

Maps and Their Uses in Programming

Maps with Dictionaries in Python

Python dictionaries double as maps:

map_example = {'key1': 'value1', 'key2': 'value2'} print(map_example['key1']) # Outputs 'value1'

Associative Arrays in PHP as Maps

Maps in PHP are implemented as associative arrays:

$map = ['key1' => 'value1', 'key2' => 'value2']; echo $map['key1']; // Outputs 'value1'

C++ Implementation of Maps

Use std::map for sorted key-value pairs or std::unordered_map for hashes:

#include <iostream> #include <map> using namespace std; int main() { map<string, string> my_map = {{"key1", "value1"}, {"key2", "value2"}}; cout << my_map["key1"] << endl; // Outputs 'value1' return 0; }

Creating Maps in Zig

Maps in Zig:

const std = @import("std"); pub fn main() void { var map = std.AutoHashMap([]const u8, []const u8).init(std.heap.page_allocator); map.put("key1", "value1"); std.debug.print("{}\n", .{map.get("key1").?}); // Outputs 'value1' }

Maps in Go

Go's map type:

package main import "fmt" func main() { myMap := map[string]string{"key1": "value1", "key2": "value2"} fmt.Println(myMap["key1"]) // Outputs 'value1' }

Final Thoughts on Data Structures

  • Arrays are fixed-size collections that are ideal for predictable data.

  • Slices offer a flexible way to view subsets of arrays.

  • Hashes/Maps provide efficient key-value storage for rapid lookups.

  • Lists are dynamic, ordered structures for resizable collections.

Choosing the correct data structure ensures better performance and cleaner code for any programming task.

Last modified: 26 January 2025