Kotlin Vector

Introduction

In Kotlin, Vector is a part of the Java standard library and can be used in Kotlin through Java interoperability. Vector is a synchronized, dynamic array that can grow as needed to accommodate additional elements. It is part of the java.util package and provides various methods to manipulate the elements in the vector.

Table of Contents

  1. What is Vector?
  2. Creating a Vector
  3. Common Operations
  4. Examples of Vector
  5. Real-World Use Case
  6. Conclusion

1. What is Vector?

Vector in Kotlin, accessed via Java interoperability, is a synchronized dynamic array that allows for elements to be added and removed as needed. It is part of the java.util package and implements the List interface, providing a resizable array that is thread-safe.

Syntax

import java.util.Vector

2. Creating a Vector

You can create a Vector using its constructor and add elements to it.

Example

val vector = Vector<Int>() // Creates an empty Vector
val vectorWithCapacity = Vector<Int>(10) // Creates a Vector with initial capacity 10
val vectorFromCollection = Vector(listOf(1, 2, 3)) // Creates a Vector from a collection

3. Common Operations

Vector supports various operations for adding, removing, and accessing elements.

Adding Elements

  • add(element: E): Adds an element to the end of the vector.
  • add(index: Int, element: E): Adds an element at the specified index.
  • addAll(elements: Collection<E>): Adds all elements from the specified collection to the vector.

Removing Elements

  • remove(element: E): Removes the first occurrence of the specified element.
  • removeAt(index: Int): Removes the element at the specified index.
  • clear(): Removes all elements from the vector.

Accessing Elements

  • get(index: Int): Returns the element at the specified index.
  • set(index: Int, element: E): Replaces the element at the specified index with the specified element.
  • firstElement(): Returns the first element.
  • lastElement(): Returns the last element.

Checking Properties

  • size: Returns the number of elements in the vector.
  • isEmpty(): Checks if the vector is empty.
  • contains(element: E): Checks if the vector contains the specified element.
  • indexOf(element: E): Returns the index of the first occurrence of the specified element.
  • lastIndexOf(element: E): Returns the index of the last occurrence of the specified element.

Iterating Over Elements

  • forEach(action: (E) -> Unit): Performs the given action on each element.

4. Examples of Vector

Example 1: Adding Elements

add(element: E)

This example demonstrates how to add an element to the end of the vector.

fun main() {
    val vector = Vector<Int>()
    vector.add(1)
    vector.add(2)
    vector.add(3)
    println("Vector after adding elements: $vector") // Output: Vector after adding elements: [1, 2, 3]
}

Output:

Vector after adding elements: [1, 2, 3]

add(index: Int, element: E)

This example demonstrates how to add an element at a specified index.

fun main() {
    val vector = Vector<Int>()
    vector.add(0, 1)
    vector.add(1, 2)
    vector.add(1, 3) // Adds 3 at index 1
    println("Vector after adding elements at specific index: $vector") // Output: Vector after adding elements at specific index: [1, 3, 2]
}

Output:

Vector after adding elements at specific index: [1, 3, 2]

Example 2: Removing Elements

remove(element: E)

This example demonstrates how to remove the first occurrence of a specified element.

fun main() {
    val vector = Vector(listOf(1, 2, 3, 2))
    vector.remove(2)
    println("Vector after removing element 2: $vector") // Output: Vector after removing element 2: [1, 3, 2]
}

Output:

Vector after removing element 2: [1, 3, 2]

removeAt(index: Int)

This example demonstrates how to remove the element at a specified index.

fun main() {
    val vector = Vector(listOf(1, 2, 3))
    vector.removeAt(1)
    println("Vector after removing element at index 1: $vector") // Output: Vector after removing element at index 1: [1, 3]
}

Output:

Vector after removing element at index 1: [1, 3]

Example 3: Accessing and Modifying Elements

get(index: Int)

This example demonstrates how to get the element at a specified index.

fun main() {
    val vector = Vector(listOf(1, 2, 3))
    println("Element at index 1: ${vector[1]}") // Output: Element at index 1: 2
}

Output:

Element at index 1: 2

set(index: Int, element: E)

This example demonstrates how to replace the element at a specified index with the specified element.

fun main() {
    val vector = Vector(listOf(1, 2, 3))
    vector[1] = 4
    println("Vector after setting element at index 1 to 4: $vector") // Output: Vector after setting element at index 1 to 4: [1, 4, 3]
}

Output:

Vector after setting element at index 1 to 4: [1, 4, 3]

firstElement() and lastElement()

This example demonstrates how to get the first and last elements of the vector.

fun main() {
    val vector = Vector(listOf("apple", "banana", "cherry"))
    println("First element: ${vector.firstElement()}") // Output: First element: apple
    println("Last element: ${vector.lastElement()}") // Output: Last element: cherry
}

Output:

First element: apple
Last element: cherry

Example 4: Checking Properties

size, isEmpty(), and contains(element: E)

This example demonstrates how to check the size of the vector and if it contains a specific element.

fun main() {
    val vector = Vector(listOf(1, 2, 3))
    println("Size of vector: ${vector.size}") // Output: Size of vector: 3
    println("Is vector empty: ${vector.isEmpty()}") // Output: Is vector empty: false
    println("Vector contains 2: ${vector.contains(2)}") // Output: Vector contains 2: true
}

Output:

Size of vector: 3
Is vector empty: false
Vector contains 2: true

Example 5: Iterating Over Elements

forEach(action: (E) -> Unit)

This example demonstrates how to iterate over the elements in the vector.

fun main() {
    val vector = Vector(listOf("apple", "banana", "cherry"))
    vector.forEach { println(it) }
}

Output:

apple
banana
cherry

5. Real-World Use Case: Managing a List of Tasks

You can use Vector to manage a list of tasks in a task management application.

Example: Managing Tasks

fun main() {
    val tasks = Vector<String>()
    tasks.add("Task 1")
    tasks.add("Task 2")
    tasks.add("Task 3")
    println("Initial tasks: $tasks") // Output: Initial tasks: [Task 1, Task 2, Task 3]

    tasks.add(1, "Task 1.5")
    println("Tasks after adding Task 1.5: $tasks") // Output: Tasks after adding Task 1.5: [Task 1, Task 1.5, Task 2, Task 3]

    tasks.remove("Task 2")
    println("Tasks after removing Task 2: $tasks") // Output: Tasks after removing Task 2: [Task 1, Task 1.5, Task 3]

    tasks[0] = "Task 0"
    println("Tasks after updating Task 1 to Task 0: $tasks") // Output: Tasks after updating Task 1 to Task 0: [Task 0, Task 1.5, Task 3]

    tasks.clear()
    println("Tasks after clearing: $tasks") // Output: Tasks after clearing: []
}

Output:

Initial tasks: [Task 1, Task 2, Task 3]
Tasks after adding Task 1.5: [Task 1, Task 1.5, Task 2

, Task 3]
Tasks after removing Task 2: [Task 1, Task 1.5, Task 3]
Tasks after updating Task 1 to Task 0: [Task 0, Task 1.5, Task 3]
Tasks after clearing: []

Conclusion

The Vector class in Kotlin, accessible via Java interoperability, is a powerful and flexible way to manage dynamic arrays with synchronized access. It is part of the java.util package and provides essential operations for accessing, querying, and modifying elements. Understanding and utilizing the Vector class can greatly enhance your ability to work with dynamic collections in Kotlin.

Comments