Dynamic Arrays (ArrayList, Vector)

beginner
22 min

Dynamic Arrays (ArrayList, Vector)

Welcome to our deep dive into the fascinating world of Dynamic Arrays! In this lesson, we'll explore two popular implementations of dynamic arrays – ArrayList and Vector. By the end of this tutorial, you'll be well-equipped to use these powerful tools in your programming projects.

Let's start by understanding why we need dynamic arrays.

What are Dynamic Arrays?

A dynamic array (or resizable array) is a type of data structure that can change size dynamically during program execution. Unlike a fixed-size array, a dynamic array can grow or shrink as needed to accommodate more elements. This property makes dynamic arrays ideal for handling situations where the number of elements is unknown or changes frequently.

In this lesson, we'll focus on two popular implementations of dynamic arrays:

  1. ArrayList – A dynamic array implementation available in Java and C#.
  2. Vector – A dynamic array implementation found in Java and C++ Standard Template Library (STL).

Why Use Dynamic Arrays?

Dynamic arrays offer several advantages over fixed-size arrays:

  1. Flexibility: Dynamic arrays can grow and shrink as needed, making them ideal for handling varying data sizes.
  2. Efficiency: Dynamic arrays provide fast access to elements, as they're based on an underlying array data structure.
  3. Simplicity: Working with dynamic arrays is intuitive and straightforward, making them accessible to beginners and intermediates alike.

Now that we've set the stage, let's dive into the details!

ArrayList

What is ArrayList?

In Java, the ArrayList is a dynamic array implementation that can store elements of any data type (object). It's part of the Java Collections Framework and offers many useful methods for handling arrays dynamically.

Creating an ArrayList

To create an ArrayList, use the following syntax:

java
import java.util.ArrayList; ArrayList<YourType> yourArrayList = new ArrayList<YourType>();

Replace YourType with the data type you want to store in your ArrayList. For example, if you want to create an ArrayList to store integers, use ArrayList<Integer>.

Adding Elements to an ArrayList

You can add elements to an ArrayList using the add() method:

java
ArrayList<Integer> myArrayList = new ArrayList<Integer>(); myArrayList.add(5); myArrayList.add(10); myArrayList.add(15);

Accessing Elements in an ArrayList

To access an element in an ArrayList, use its index number:

java
int thirdElement = myArrayList.get(2); // Access the third element

Removing Elements from an ArrayList

To remove an element from an ArrayList, use the remove() method:

java
myArrayList.remove(1); // Remove the second element

ArrayList Example

Here's an example that demonstrates working with an ArrayList:

java
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> myArrayList = new ArrayList<Integer>(); myArrayList.add(5); myArrayList.add(10); myArrayList.add(15); System.out.println("Initial ArrayList: " + myArrayList); myArrayList.add(20); System.out.println("ArrayList after adding an element: " + myArrayList); int thirdElement = myArrayList.get(2); System.out.println("Third element: " + thirdElement); myArrayList.remove(1); System.out.println("ArrayList after removing an element: " + myArrayList); } }

When you run this code, you'll see the following output:

Initial ArrayList: [5, 10, 15] ArrayList after adding an element: [5, 10, 15, 20] Third element: 15 ArrayList after removing an element: [5, 20]
Quick Quiz
Question 1 of 1

What is the output of the following ArrayList example?

Vector

What is Vector?

In Java and C++, the Vector is another implementation of a dynamic array. Unlike ArrayList, Vector can only store elements of type Object. In Java, Vector is part of the Java Collections Framework, while in C++, it's part of the Standard Template Library (STL).

Creating a Vector

To create a Vector, use the following syntax:

java
import java.util.Vector; Vector<YourType> yourVector = new Vector<YourType>();

Replace YourType with the data type you want to store in your Vector.

Adding Elements to a Vector

You can add elements to a Vector using the add() method:

java
Vector<Integer> myVector = new Vector<Integer>(); myVector.addElement(5); myVector.addElement(10); myVector.addElement(15);

Accessing Elements in a Vector

To access an element in a Vector, use its index number:

java
int thirdElement = myVector.elementAt(2); // Access the third element

Removing Elements from a Vector

To remove an element from a Vector, use the removeElement() method:

java
myVector.removeElement(1); // Remove the second element

Vector Example

Here's an example that demonstrates working with a Vector:

java
import java.util.Vector; public class VectorExample { public static void main(String[] args) { Vector<Integer> myVector = new Vector<Integer>(); myVector.addElement(5); myVector.addElement(10); myVector.addElement(15); System.out.println("Initial Vector: " + myVector); myVector.addElement(20); System.out.println("Vector after adding an element: " + myVector); int thirdElement = myVector.elementAt(2); System.out.println("Third element: " + thirdElement); myVector.removeElement(1); System.out.println("Vector after removing an element: " + myVector); } }

When you run this code, you'll see the following output:

Initial Vector: [5, 10, 15] Vector after adding an element: [5, 10, 15, 20] Third element: 15 Vector after removing an element: [5, 20]
Quick Quiz
Question 1 of 1

What is the output of the following Vector example?

Congratulations on mastering dynamic arrays with ArrayList and Vector! Now that you understand how to use these powerful tools, you're well on your way to becoming a proficient programmer. Keep learning and practicing, and soon you'll be creating amazing projects with ease! šŸŽÆ