Welcome to our Java Arrays Tutorial! In this section, we'll explore the basics of arrays in Java, including how to declare, initialize, and manipulate arrays. Arrays are a fundamental data structure in Java, used to store multiple values of the same type in a single variable.

What are Arrays?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, the length of the array cannot be changed. Here's an example of how to declare and initialize an array:

int[] numbers = {1, 2, 3, 4, 5};

Accessing Elements

Each element in an array has an index, starting from 0. You can access an element by using its index:

int firstNumber = numbers[0]; // 1

Modifying Elements

You can modify the value of an element by using its index:

numbers[2] = 10;

Array Operations

Java provides a variety of methods to work with arrays, such as length, toString, and clone. Here are some of the most commonly used array operations:

  • length: Returns the length of the array.
  • toString: Returns a string representation of the array.
  • clone: Creates a copy of the array.

For more information on array operations, please refer to the Java Arrays API documentation.

Array Examples

Let's take a look at a few examples of array usage in Java:

// Example 1: Declaring and initializing an array
String[] fruits = {"Apple", "Banana", "Cherry"};

// Example 2: Accessing and modifying an element
fruits[0] = "Orange";

// Example 3: Using the length property
System.out.println("Number of fruits: " + fruits.length);

Further Reading

To learn more about arrays in Java, we recommend checking out the following resources:

For additional Java tutorials, please visit our Java Learning Center.


Here's an image of an array in action:

Java_Arrays