Step 6 – Array and tuple types in TypeScript

image 2

Play Store Application link – Java to TypeScript in 14 Steps – App on Google Play

Arrays and tuples are fundamental data structures in TypeScript, each serving different purposes. Arrays are used for collections of similar items, while tuples are used for fixed collections of items with different types.

Array Types

In TypeScript, arrays are used to store collections of values of the same type. You can define and work with arrays using two main syntaxes:

  1. Array Syntax: Using [] to specify the type of elements. let myArray: number[]; // declare an array of numbers let myArray: number[] = [1, 2, 3, 4, 5]; // create an array of numbers This syntax is similar to arrays in Java, where you declare an array with a type: int[] myArray = {1, 2, 3, 4, 5}; // create an array of integers
  2. Generic Array Syntax: Using Array<type> to define the array. let myArray: Array<number>; // declare an array of numbers using the Array keyword let myArray: Array<string> = ['foo', 'bar', 'baz']; // create an array of strings In Java, you use generics with collections: List<String> myArray = Arrays.asList("foo", "bar", "baz"); // create a list of strings

Accessing Array Elements

Array elements are accessed using their index. The index starts from 0.

let myArray: number[] = [1, 2, 3, 4, 5];
console.log(myArray[0]); // output: 1
console.log(myArray[4]); // output: 5

In Java:

int[] myArray = {1, 2, 3, 4, 5};
System.out.println(myArray[0]); // output: 1
System.out.println(myArray[4]); // output: 5

Iterating Over Arrays

You can iterate over arrays using loops or the forEach method.

let myArray: number[] = [1, 2, 3, 4, 5];
for (let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

myArray.forEach(element => {
  console.log(element);
});

In Java:

for (int i = 0; i < myArray.length; i++) {
  System.out.println(myArray[i]);
}

Arrays.asList(myArray).forEach(System.out::println);

Adding and Removing Elements

You can add elements using push() and concat(), and remove them using pop() and splice().

let myArray: number[] = [1, 2, 3, 4, 5];
myArray.push(6); // add 6 to the end of the array
console.log(myArray); // output: [1, 2, 3, 4, 5, 6]

let newArray: number[] = myArray.concat([7, 8, 9]); // concatenate two arrays
console.log(newArray); // output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

myArray.pop(); // remove the last element
console.log(myArray); // output: [1, 2, 3, 4]

myArray.splice(2, 1); // remove one element starting at index 2
console.log(myArray); // output: [1, 2, 4]

In Java:

List<Integer> myArray = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
myArray.add(6); // add 6 to the end of the list
System.out.println(myArray); // output: [1, 2, 3, 4, 5, 6]

List<Integer> newArray = new ArrayList<>(myArray);
newArray.addAll(Arrays.asList(7, 8, 9)); // concatenate two lists
System.out.println(newArray); // output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

myArray.remove(myArray.size() - 1); // remove the last element
System.out.println(myArray); // output: [1, 2, 3, 4]

myArray.remove(2); // remove the element at index 2
System.out.println(myArray); // output: [1, 2, 4]

Tuple Types

Tuples are used for fixed-size arrays where elements can be of different types. Define a tuple using [] with types for each position.

let myTuple: [number, string] = [1, 'hello']; // create a tuple with a number and a string

In Java, tuples aren’t built-in, but you can use arrays with generics or custom classes for similar functionality:

public class Tuple {
  public final int number;
  public final String text;

  public Tuple(int number, String text) {
    this.number = number;
    this.text = text;
  }
}

Accessing and Updating Tuple Elements

You can access and update elements in a tuple using indices.

let myTuple: [number, string] = [1, 'hello'];
console.log(myTuple[0]); // output: 1
console.log(myTuple[1]); // output: 'hello'

myTuple[0] = 2;
console.log(myTuple); // output: [2, 'hello']

In Java:

Tuple myTuple = new Tuple(1, "hello");
System.out.println(myTuple.number); // output: 1
System.out.println(myTuple.text); // output: 'hello'

Adding and Removing Elements from Tuples

Unlike arrays, tuples have fixed sizes and types, so you cannot add or remove elements after creation.

Tuple vs. Array Types

  • Tuple: Fixed size, different types for each element. let myTuple: [number, string] = [1, 'hello'];
  • Array: Variable size, same type for all elements. let myArray: number[] = [1, 2, 3, 4, 5];

In TypeScript:

myTuple[2] = true; // Error: Property '2' does not exist on type '[number, string]'
myArray.push('hello'); // Error: Argument of type '"hello"' is not assignable to parameter of type 'number'

In Java, similar restrictions apply to fixed-size arrays versus lists.

Diagram

Tuple:   [ 1,  'hello' ]
          ^      ^
          |      |
          0      1

Array:   [ 1, 2, 3, 4, 5 ]
          ^           ^
          |           |
          0           4

In the tuple, each index has a specific type, while in the array, all elements share the same type.

Leave a Reply

Your email address will not be published. Required fields are marked *