DEV Community

Edwin Torres
Edwin Torres

Posted on

Basic Arrays in Java

The array is one of the fundamental and most useful concepts in programming. Most programming languages let programmers create and use arrays. Java is one of them.

An array is a group of variables that have the same name and data type. To access one of the variables, use the array name and the integer index of the variable. In Java, array indexes start with 0. Arrays have a fixed length. Java implements arrays as objects.

To declare an array named arr that can hold integer values:

int[] arr;
Enter fullscreen mode Exit fullscreen mode

In the code above, arr is the variable name. int[] is the type, which is an array of integer values. This code only declares the arr variable. Here is how to allocate an array object with 5 integer elements:

int[] arr = new int[5];
Enter fullscreen mode Exit fullscreen mode

In this code, new is a Java keyword that allocates memory for an object and int[5] specifies an array of 5 integer values. Since the default value for Java int variables is 0, the array contains five 0 values in positions 0-4.

Here is another way to allocate the same array:

int[] arr = new int[]{0,0,0,0,0};
Enter fullscreen mode Exit fullscreen mode

The array object has a length property to indicate the size of the array:

int len = arr.length;
Enter fullscreen mode Exit fullscreen mode

In this array example, arr.length would return 5.

To access an individual element of an array, supply an index to the array variable. The index is in square brackets [ ] after the array name:

arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
arr[3] = 400;
arr[4] = 500;

System.out.println( arr[3] ); // 400
Enter fullscreen mode Exit fullscreen mode

The first five lines assign the values 100, 200, 300, 400, and 500 to array positions 0, 1, 2, 3, and 4 respectively. Note that array elements are like integer variables, except with integer indexes.

The last line outputs the array element at position 3: 400. As you can see, an array element can be wherever an integer value is allowed.

Since the array indexes form a sequence of integers, arrays work well with loops. For an array of length 5, it has the indexes 0-4. Here is an example that uses a for loop to output each element of our array:

for (int i=0; i < arr.length; i++) {
  System.out.println(arr[i]);
}
Enter fullscreen mode Exit fullscreen mode

And here is the output:

100
200
300
400
500
Enter fullscreen mode Exit fullscreen mode

This example uses the loop variable i as the index of the array. The loop repeats its statement five times, once for each index of the array.

What if we want to sum all the elements in the array? It's easy. Use a loop:

int sum = 0;
for (int i=0; i < arr.length; i++) {
  sum = sum + arr[i];
}

System.out.println(sum); // 1500
Enter fullscreen mode Exit fullscreen mode

Here is the complete program:

public class Example {
  public static void main(String[] args) throws Exception {

    int[] arr = new int[]{0,0,0,0,0};

    arr[0] = 100;
    arr[1] = 200;
    arr[2] = 300;
    arr[3] = 400;
    arr[4] = 500;

    int sum = 0;
    for (int i=0; i < arr.length; i++) {
      sum = sum + arr[i];
    }

    System.out.println(sum); // 1500
  }
}
Enter fullscreen mode Exit fullscreen mode

Arrays are very useful in programming. Think of an array like a group of variables, all with the same name and data type. Use an index to access each element. You can assign a value to an element of the array or access the value of an element, just like plain variables. Use arrays with loops to see the true power of arrays.

Thanks for reading.

Follow me on Twitter @realEdwinTorres for more programming tips. 😀

Top comments (0)