Java development for beginners

Basics of One-Dimensional Arrays in Java

All videos of the tutorial Java development for beginners

One-dimensional arrays are a fundamental concept in the programming language Java. They allow the storage of lists of data of the same type, which makes many applications in programming easier. In this tutorial, you will learn how to declare and initialize arrays, how to access their elements, and why it is important to calculate the indices correctly.

Main findings

  • Arrays are lists of similar data types.
  • The declaration and initialization of an array can occur simultaneously.
  • Accessing the elements of an array is done through their indices, which start at zero.

Step-by-step guide

First of all, you should understand that arrays in Java are a collection of values of a specific type. They can contain simple data types like int, String, or complex data types.

Basics of one-dimensional arrays in Java

1. Declaration and initialization of an array

To create a one-dimensional array, we start with the declaration and initialization. You must first specify the type of the array, followed by square brackets and the name of the array. In our example, we will create an array for the months of the year.

Basics of one-dimensional arrays in Java

2. Assigning values

After the array has been declared and initialized, you can assign values to the individual elements. We start with the first element and work our way through the array.

Fundamentals of One-Dimensional Arrays in Java

Repeat this for each month, adjusting the indices accordingly: months[1], months[2], and so on up to the twelfth month months[11] for December.

Basics of one-dimensional arrays in Java

3. Indices and their significance

It is important to understand that indices in Java start at zero. This means that the first element is stored at index 0, the second at index 1, etc. You can use this to access the corresponding values.

Fundamentals of one-dimensional arrays in Java

The calculation of the index is done by: position - 1.

So we know that January is at index 0 and February is at index 1.

4. Debugging the array

Now that we have assigned some values, we want to check if everything is correct. You can use the debugger in your development environment to view the contents of the array.

Basics of one-dimensional arrays in Java

When you start the debugger, you can see that the values in the array are set correctly. Your months array should now contain the months from January to December.

Basics of one-dimensional arrays in Java

5. Accessing the elements

To test whether the assignments are correct, you can output a value. Accessing an element works the same way as assigning a value. For example, you can use System.out.println(months[0]); to output the first month – January.

Fundamentals of One-Dimensional Arrays in Java

When you run the program, the output "January" should appear, confirming that the assignment was correct.

Summary - One-dimensional arrays in Java: The basics for beginners

In this tutorial, you learned how one-dimensional arrays are declared, initialized, and used in Java. You now know that accessing elements is done via indices that start at zero. You also got an insight into debugging to check the values of your array.

Frequently Asked Questions

How do I declare a one-dimensional array in Java?You declare an array by writing the type followed by square brackets and the name, for example String[] months.

How can I add values to an array?You simply assign the values their respective index, like months[0] = "January";.

Why do indices start at zero?Indices start at zero to ensure consistent and mathematically grounded counting.

What happens if I access a non-existent index?Accessing a non-existent index results in an ArrayIndexOutOfBoundsException error.