Arrays/Lists

Arrays and lists in computer programming serve as fundamental data structures for storing collections of elements, providing versatility, efficiency, and ease of manipulation. They allow programmers to group related data together, enabling convenient access, retrieval, and modification of individual elements based on their indices. Arrays and lists are widely used in various programming tasks, including storing and processing large datasets, implementing algorithms, managing dynamic collections of data, and representing multidimensional data structures. It is important to note that in some languages like R, arrays are not mutable and are fixed in size and in terms of value of array entries. The examples here are focused more on mutable list type of data structure.

Select Languages

Examples

C

char* names[] = {"Joe", "Alex", "Bob"};
names[1] = "Tom";
char* name = names[1];
int numItems = sizeof(names) / sizeof(names[0]);

printf("%s\n", name);
printf("%d", numItems);

C#

string[] names = new string[] {"Joe", "Alex", "Box"};
names[1] = "Tom";
string name = names[1];
int numItems = names.Length;

Console.WriteLine(name);
Console.WriteLine(numItems);

C++

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> names = {"Joe", "Alex", "Bob"};

names[1] = "Tom";
string name = names[1];
int numItems = names.size();
cout << name << endl;
cout << numItems << endl;

Go

package main
import "fmt"

var names = [...]string{"Joe", "Alex", "Bob"}
names[1] = "Tom"
var name string = names[1]
var numItems int = len(names)

fmt.Println(name)
fmt.Println(numItems)

Java

String[] names = {"Joe", "Alex", "Bob"};
names[1] = "Tom";
String name = names[1];
int numItems = names.length;

System.out.println(name);
System.out.println(numItems);

JavaScript

const names = ['Joe', 'Sam', 'David'];
names[1] = "Tom";
const name = names[1];
const numItems = names.length;

console.log(name);
console.log(numItems);

Kotlin

var names = 
  mutableListOf("Joe", "Alex", "Bob");
names.set(1, "Tom");
val name = names.get(1);
val numItems = names.size;
    
println(name);
println(numItems);

MatLab

names = ["Joe", "Alex", "Bob"];
names(2) = "Tom";
name = names(2);
numItems = size(names);

disp(name);
disp(numItems);

PHP

$names = array("Joe", "Alex", "Bob");
$names[1] = "Tom";
$name = $names[1];
$numItems = sizeof($names);

echo "$name \n";
echo $numItems;

Python

names = ["Joe", "Alex", "Bob"]
names[1] = "Tom"
name = names[1]
numItems = len(names)

print(name)
print(numItems)

R

names <- list("Joe", "Alex", "Bob")
names[2] = "Tom"
name = names[2]
numItems = length(names)

print(name)
print(numItems)

Ruby

names = Array["Joe", "Alex", "Bob"]
names[1] = "Alex"
name = names[1]
numItems = names.length

puts name
puts numItems

Rust

let mut names = vec!["Joe", "Alex", "Bob"];
std::mem::replace(&mut names[1], "Tom");
let name = names[1];
let numItems = names.len();
    
println!("{}", name);
println!("{}", numItems);

Scala

var names = Array("Joe", "Sam", "David");
names(1) = "Tom";
var name = names(1);
var numItems = names.length;

println(name);
println(numItems);

Swift

var names = ["Joe", "Sam", "David"];
names[1] = "Tom";

// constant variable
let name = names[1];
let numItems = names.count;

print(name);
print(numItems);

TypeScript

const names:string[] =
  ['Joe', 'Sam', 'David'];

names[1] = "Tom";

const name:string = names[1];
const numItems:number = names.length;

console.log(name);
console.log(numItems);

Copyright 2025. All Rights Reserved. IronCodeMan.