# Array type in Java

In 
Published 2024-01-06

This tutorial explains to you how to use Array type in Java. We have an example for you as well.

# What is an array ?

An array is a container object that holds a fixed number of values of a single type. We must specify the length of an array during the definition/creation of the array (Java needs to know the length of the array when the memory is allocated).

- Ways to define an array in Java
// EXAMPLE #1
// declares an array of integers
int[] myArray1;

// allocates memory for 10 integers
myArray1 = new int[4];

// initialize the elements
myArray1[0] = 111;
myArray1[1] = 200;
myArray1[2] = 300;
myArray1[3] = 400;

// EXAMPLE #2
// declares, allocates memory for an array of Strings
String myArray2[] = new String[4];
// initialize the array
myArray2[0] = "Hello";

// EXAMPLE #3
// declares, allocates memory, initialize an array of Strings
String[] myArray3 = {"Peugeot", "Mazda", "Ford", "Fiat"};

If we need to read or to update a value of a position in an array, we can do it directly:

String[] myArray1 = {"Peugeot", "Mazda", "Ford", "Fiat"};

System.out.println("myArray1[0]="+myArray1[0]);
myArray1[0] = "Mazda";
System.out.println("myArray1[0]="+myArray1[0]);

When we run this code we can see :

myArray1[0]=Peugeot
myArray1[0]=Mazda

# How can we sort an array ?

Sorting is very simple. We need to use the sort() method of the Arrays class.

Here we have an example:

String[] myArray1 = {"Peugeot", "Mazda", "Ford", "Audi"};
System.out.println("myArray1[0]="+myArray1[0]);
System.out.println("-------------------------------");
Arrays.sort(myArray1);
System.out.println("myArray1[0]="+myArray1[0]);
System.out.println("myArray1[1]="+myArray1[1]);

# How can we extend an array ?

When we extend an array, the new array will be put in another location. In the same location, an array cannot be extended.

If we need to extend an array in the code we can use the following algorithm:

String[] myArray = {"Peugeot", "Mazda", "Ford", "Audi"};
System.out.println("myArray.length="+myArray.length);
System.out.println("myArray="+myArray);

String tmpArray[] = new String[10];
System.out.println("tmpArray="+tmpArray);
for (int i = 0; i < myArray.length; i++) {
    tmpArray[i] = myArray[i];
}

System.out.println("------------------");
myArray = tmpArray;
System.out.println("myArray="+myArray);
System.out.println("myArray.length="+myArray.length);

tmpArray=null;
System.out.println("tmpArray="+tmpArray);

# How can we use a 2 dimensional array ?

This is simple, and we can take a look to the example below:

// define the array
int[][] arr = {{1,2,3}, {4,5,6}};

// read a value from an array
int val1 = arr[1][0];
System.out.println("val1="+val1);

// update a value in an array
arr[1][0] = 10;

// As you can see, the multidimensional arrays are a sum of objects 
// (stored in HEAP)
System.out.println("arr[0]="+arr[0]);
System.out.println("arr[1]="+arr[1]);

When we run this, we will see something like this:

val1=4
arr[0]=[I@1a2e2935
arr[1]=[I@733c423e

# Array <--> List conversion

Here we have some examples:

// Array ---> List
int[] myArray1 = {10, -7, -8, 3, 2, 0, -1};

List<Integer> listFromArray1 =Arrays.stream(myArray1).boxed().collect(Collectors.toList());
ArrayList<Integer> listFromArray11 =Arrays.stream(myArray1).boxed().collect(Collectors.toCollection(ArrayList::new));

// An ArrayList could be created using a manual casting as well
ArrayList<Integer> listFromArray2 = (ArrayList<Integer>) Arrays.stream(myArray1).boxed().collect(Collectors.toList());

System.out.println("listFromArray1.size="+listFromArray1.size());
System.out.println("listFromArray2.size="+listFromArray2.size());

// List ---> Array
// Java 8 style
Integer[] myArray2 = listFromArray2.stream().toArray(Integer[]::new);

//Java 11 style
Integer[] myArray3 = listFromArray2.toArray(Integer[]::new);

System.out.println("myArray2.length="+myArray2.length);
System.out.println("myArray3.length="+myArray3.length);

when we run we con see something like this :

listFromArray1.size=7
listFromArray2.size=7
myArray2.length=7
myArray3.length=7

# Exercises #1 - Sum the positive number in an array

int[] myArray1 = {10, -7, 1, 20, -1};

Integer sum = Arrays.stream(myArray1)
        .boxed()
        .filter( i -> i > 0)
        .reduce(0, (subtotal, element) -> subtotal + element);

System.out.println("sum = " + sum);

# Exercises #2 - Is any negative number in an array ?

int[] myArray1 = {10, 7, 1, 20, 1};

Boolean anyPositive = Arrays.stream(myArray1).anyMatch(it -> it < 0);

System.out.println("anyPositive = " + anyPositive);