Array vs. ArrayList in Java. No matter which programming language…
Tags: Java, Programming, Coding, Object Oriented, Software Development

Source: clearbyte.ch

No matter which programming language you want to master — you can’t get around arrays. This also applies to Java.

And it doesn’t matter if you already know arrays pretty well or if you don’t know what arrays are — this article is for you.

I will go through the concept and implementation of arrays in Java with you.

Because there is also the so-called ArrayList in Java, which is very important to understand, we will also go through this feature.

We will especially clarify the differences between Arrays and ArrayLists.

Have fun!

What are Java’s Arrays?

You may already know arrays from other programming languages — because they are so fundamental that they exist in almost every language.

You may already know arrays from other programming languages — because they are so fundamental that they exist in almost every language. Very simplified we can imagine an array like a list or a stack of elements. Each element could be thought of as a variable of its own, which we can access with code and which has a value.

We also declare an array as a variable — but it can store several independent values.

For example, we could create an array called “Students”, which stores the names “Max”, “Anna” and “Tom”. We could also create a variable for each of the students, but that would be very difficult to manage. Because we can not deduce from one variable in the code to another.

If the variable students1 exists in the code, we cannot use code to determine whether there are still students2 or students3. In an array, however, we have all elements in one place.

It is important to understand that although array is a term that occurs in different programming languages, the technical details differ from language to language.

Arrays have a fixed size, so we cannot simply add another element to an array.

Arrays can store arbitrary primitive data types and also reference types as elements.

The values of the elements in an array can be changed at any time.

The elements can be of only one data type.

The elements in an array all have an index. The index is 0 for the first element, the second element has index 2.

Let’s have a look at Java’s Arrays in practice.

With the following code, we create an empty array. The following lines make sure that we add some values to this array.

The 3 in the square brackets when creating the array means that the array only has space for 3 elements. As already mentioned, arrays have a fixed size.

String people[] = new String[3];people[0] = “Max”;

people[1] = “Anna”;

people[2] = “Tom”;

By the people[0] we indicate that we want to change the first element in the array. The first element always has the index 0, as already mentioned. We do the same for the second and third elements, which have index 1 & 2. So you notice that the length of the array is always 1 greater than the highest index in the array because we start counting from 0.

Another way to create an empty array is, instead of writing the square brackets behind the variable names, to write them directly behind the datatypes.

String[] people = new String[3];

But there is another syntax to create an empty array:

String people[];people = new String[3];people[0] = “Max”;

people[1] = “Anna”;

people[2] = “Tom”;

So far we have only created one array, which is empty by default. But we can also specify which values should be stored in the array when we create it.

String people[] = { “Max”, “Anna”, “Tom” };

Again, it does not matter if we put the square brackets after the variable-names or after the datatypes. But if we use this syntax with the curly braces, we have to pass at least one value — we cannot create an empty array by this syntax.

Outputting elements of an array

In a Java Array, you start counting from 0 — so the first element in an array has the index 0.

String[] people = { “Max”, “Anna” };System.out.println(people[0]); // Max

System.out.println(people[1]); // Anna

If we want to output a whole array with System.out.println, we have to do this with the Java Arrays-package.

import java.util.Arrays;String[] people = { “Max”, “Anna” };

System.out.println(Arrays.toString(people)); // [Max, Anna]

But it is rather rare that we want to output the whole array with its structure — we rather want to output every single element. For this, we can use the for-loop.

String[] people = { “Max”, “Anna”, “Tom” };for (int i = 0; i < people.length; i++) {

System.out.println(people[i]);

}

Now a name is printed in each line. Max, Anna, Tom.

As you saw, people.length prints the length of an array. That means the number of elements.

We can overwrite an element in an array at any time.

String[] people = { “Max”, “Anna”, “Tom” };people[0] = “Maxi”;

System.out.println(people[0]); // Maxi

Understanding what happens under the hood

Arrays under the hood often work differently in different programming languages. Let’s try to get some clarity about Java’s arrays.

When we create a new, empty array, memory is already reserved in memory for the execution of the program. Therefore we need to specify the length of the array, i.e. the maximum number of elements, which are written into our array.

The same is true for the datatype because this is essential for preparing the needed memory.

An array can be overwritten — we can create an array with the same name as our existing array. Let’s do this with an example. We create our array of people again and write two elements into it.

String people[];people = new String[2];people[0] = “Max”;

people[1] = “Anna”;

Now we create the same array directly under it, but this time with 3 elements that have room in it. At third place (with index 2) we write another name.

people = new String[3];people[2] = “Tom”;

Puzzle: What happens to Max and Anna now? Did we just extend the array, which already contained the two names before, or even completely overwrite it? Let’s find out by looking at what is in the array.

System.out.println(people[2]); // Tom

System.out.println(people[1]); // null

System.out.println(people[0]); // null

Before we created the same array again in the code, Max had index 0 and Anna had index 1 — now they are gone. We cannot access the previous values anymore, so we have overwritten the array.

So you can imagine the state of memory after we overwrote the array — at the position of index 0 and index 1 there are no values. Max and Anna are still present in memory a short time ago, but we can’t access them anymore because the new array starts to occupy fresh memory.

Then they are finally removed by the garbage collector.

In Java, every object has a counter that counts how many references point to the object.

Multidimensional Arrays

A multidimensional array is an array whose elements are arrays. The nice thing is, that you can see already when declaring such an array, that it is a multidimensional one.

Here is how you can visualize this:

The array, which we call multidimensional, has as elements itself normal arrays, which have different elements. Here is an example.

int[][] coordinates = new int[3][3];

Coordinates is now an array containing three arrays, which in turn contain 3 elements, all of which are integers.

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

http://landrover.gi/land/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

https://sv.tie.org/sv/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

https://sv.tie.org/sv/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb1.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb2.html

https://altagamma.it/dia/Video-burundi-mauritania-en-direct5-v-fr-fr-1bgb3.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

http://landrover.gi/land/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-00.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-01.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-02.html

https://oasislandscape.com/cap/atp/sub-Thiem-v-Tsitsipas-cbn-03.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-90.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-91.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-92.html

https://www.oasislandscape.com/cap/socc/dr--Video-Slovakia-vs-Scotland-news-93.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m0.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m1.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m2.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m3.html

https://www.oasislandscape.com/cap/gp/rex-r-v-m4.html

http://landrover.gi/land/gp/rex-r-v-m0.html

http://landrover.gi/land/gp/rex-r-v-m1.html

http://landrover.gi/land/gp/rex-r-v-m2.html

http://landrover.gi/land/gp/rex-r-v-m3.html

http://landrover.gi/land/gp/rex-r-v-m4.html

https://sv.tie.org/sv/gp/rex-r-v-m0.html

https://sv.tie.org/sv/gp/rex-r-v-m1.html

https://sv.tie.org/sv/gp/rex-r-v-m2.html

https://sv.tie.org/sv/gp/rex-r-v-m3.html

https://sv.tie.org/sv/gp/rex-r-v-m4.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es02.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es03.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es04.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es05.html

https://www.oasislandscape.com/cap/gp/E-M-v-V-es06.html

http://landrover.gi/land/gp/E-M-v-V-es02.html

http://landrover.gi/land/gp/E-M-v-V-es03.html

http://landrover.gi/land/gp/E-M-v-V-es04.html

http://landrover.gi/land/gp/E-M-v-V-es05.html

http://landrover.gi/land/gp/E-M-v-V-es06.html

To write something into this array, we have to specify the exact position again. As there are now three different arrays within the array, in which we could store something at three different places, we have to specify exactly for both.

coordinates[0][0] = 15;

So we write the value 15 to the first place of the first array.

Then we can output our array in the following way.

System.out.println(Arrays.toString(coordinates[0]));

System.out.println(Arrays.toString(coordinates[1]));

System.out.println(Arrays.toString(coordinates[2]));

Now we see the following output:

[15, 0, 0]

[0, 0, 0]

[0, 0, 0]

ArrayList — the workaround

When I explained what arrays are, it already became a rule that an array is fixed in size. So we cannot simply add any elements — when we create an array, we directly or indirectly specify the number of elements, which will fit in it. So we can directly reserve the needed memory.

Genauso wie Array ist ArrayList eine Klasse, die wir in unserem Java Code nutzen können, um eine ArrayList zu erstellen.

An ArrayList is basically very similar to an array:

The index starts at 0 and is an integer.

Just like an array we can create an ArrayList by calling “new”.

The function ArrayList() is especially important for us. If we do not pass a parameter to it, an ArrayList with an initial capacity of 10 elements is created. But we can also pass an integer to set the initial size.

So we create a first empty ArrayList and then directly add two values to it — Max and Anna.

ArrayList<String> people = new ArrayList<String>();people.add(“Max”);

people.add(“Anna”);

Now we want to output these elements from our ArrayList — what is noticeable is that we cannot simply access a value with people[0] — we have to use an extra function to access it.

System.out.println(people.get(0));

System.out.println(people.get(1));

There are also many other functions for the ArrayList that are useful. We can output the number of elements in the list with people.size() , so we can also use a for loop to go through all elements.

Instead of a for-loop we can also use for-each in Java — the syntax is a bit simpler.

for (int i = 0; i < people.size(); i++) {

System.out.println(people.get(i));

}

Instead, we can use for-each, which works as follows.

for (String i : people) {

System.out.println(i);

}

Looks a lot easier, doesn’t it?

With an array, it was so that we could always overwrite an element by accessing its index and simply setting another value in that place.

In lists, there is a function to overwrite a value at a position.

people.set(0, "Tom"); — we overwrite the value at position 0 with the value “Tom”.

But in a list we can not only add elements but also delete elements from it — there is a separate function for this too.

people.remove(2); — important to understand is, that not only the value of the element is set to zero, but the whole entry is removed from the list. So the size is reduced by 1 and the deleted element is missing every track.