DEV Community

Paul Allies
Paul Allies

Posted on

SKT: Arrays

Arrays are fundamental data structures in programming, used to store collections of elements. Let's explore how arrays work in Swift, Kotlin, and TypeScript.

Swift

In Swift, arrays are ordered collections of values of the same type. Swift arrays are value types, meaning when you copy an array, you get a completely new copy, not just a reference.

// Creating an array
var numbers: [Int] = [1, 2, 3, 4, 5]
var names = ["Alice", "Bob", "Charlie"] // Type inference

// Accessing elements
let firstNumber = numbers[0] // 1
let secondName = names[1] //Accessing index 1

// Modifying arrays
numbers.append(6) // Adds 6 to the end
numbers[1] = 10    // Changes the second element to 10

// Iterating through an array
for (index, number) in numbers.enumerated() {
    print("Index: \(index), Number: \(number)")
}
Enter fullscreen mode Exit fullscreen mode

Kotlin

In Kotlin, arrays can be created using the arrayOf() function, or by using specific array constructors for primitive types (e.g., intArrayOf(), doubleArrayOf()).

// Creating an array
val numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5)
val names = arrayOf("Alice", "Bob", "Charlie")

val numbers2: IntArray = intArrayOf(1,2,3,4,5)

// Accessing elements
val firstNumber = numbers[0] // 1
val secondName = names[1]

// Modifying arrays
//numbers[1] = 10 //val cannot be changed
val numbers3 = intArrayOf(1,2,3,4,5)
numbers3[1] = 10
// Iterating through an array
for (index, number) in numbers.withIndex() {
    println("Index: $index, Number: $number")
}
Enter fullscreen mode Exit fullscreen mode

TypeScript

TypeScript offers two ways to define arrays: using square brackets or the Array constructor. TypeScript arrays are also ordered collections, and their type system allows for strong typing.

// Creating an array
let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
let numbers2: Array<number> = [1,2,3,4,5];

// Accessing elements
let firstNumber = numbers[0]; // 1
let secondName = names[1];

// Modifying arrays
numbers.push(6);     // Adds 6 to the end
numbers[1] = 10;    // Changes the second element to 10

// Iterating through an array
for (let index = 0; index < numbers.length; index++) {
  console.log(`Index: ${index}, Number: ${numbers[index]}`);
}

Enter fullscreen mode Exit fullscreen mode

Key Similarities

  • Ordered Collections: All three languages use arrays to store elements in a specific order.
  • Element Access: Elements are accessed using an index, starting from 0.
  • Iteration: Similar looping mechanisms are available to iterate through array elements.

Key Differences

  • Type Specificity: Swift and Kotlin require that all elements in an array have the same type, and while Typescript is also type-safe, it offers more flexibility due to its JavaScript heritage.
  • Mutability: Swift and Kotlin differentiate more explicitly between mutable and immutable arrays.
  • Syntax: While the basic concepts are the same, the syntax for declaring, accessing, and modifying arrays varies slightly between the languages.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay