Home / General programming / JAVA / Array in JAVA

Array in JAVA

Array in java

At the post office, we could rent a box in order to receive our letters. There are many other places where we can rent a box: at the bank, in order to store valuables, at the train station, in order to leave a suitcase.
Boxes are typically identified by consecutive numbers. The boxes or lockers might not all be of the same size. The things we might want to store might require that.
In programming, we have seen variables that allow us to store values. Here, the sizes might also be different depending on whether we want to store a Boolean value or a floating point number. On occasions, we might need an ordered collection of values of the same type. For instance, when we want to store the grades of the students of a class, or the temperatures of each day of a month in a given location. In the same way as we could rent a row of boxes, we could need to reserve an array of variables of the same type.
How do we refer to them?
We’re used to freely selecting the names of variables. And in the same way, we can give a name to an array of them. To refer to one single location, induces are used. So for instance, we could call an array a. Assume it has four elements, four places. Then we would refer to each position by adding an index in square brackets.



In this example, we would have a[0], a[1], a[2], and so on.Note that we’ll start with index 0 and increment one by one.
Here, we see examples of arrays.

[1,3,5,7,9] [2,0,1,5] [true,false,true] []

The array in java can contain different types of values, but in each array in java, the type is the same for all values. The array could be of any length, but once defined the length at the declaration, its length remains fixed.

Here we have a summary of arrays.

array in java
Elements in an array can be accessed through indicts.
We should not confuse the value of an element with its index. In this example, the value of a[2] is 1. Another thing to remember is that the first element is index 0. So indicts run from 0 to the length -1.
Here’s how we declare an array.

Declaration array in java
We write the type of the element, then open and close square brackets, and then the name we have chosen for our array. We see that the base type can be any type, and we have highlighted the most relevant ones which we will use in this course.
Now, one thing is to give a name to an array, or to declare it, and another one, to physically reserve space for it, like in the post office or in computer memory, as in our case. This is done by means of a creation statement, like below, a equals new int 4.

Creation array in java


This is the notation to reserve space in memory.
Note the use of new. We will come back to that later.
We can do the two things, declaration and creation, in one line as we see here in the lower part.


error array in java
To access an element of the array, we use the square brackets as we see here.
Remember that if we have declared and created an array of integers of size four, its first element is a[0], and the last one is a[3]. a[4] would not be defined. It would be an error.

So we have given a name and we have reserved space for an array.Now we want to store values in the different positions.

How do we store values?

Assignment array in java

We use the assignment statement as we have used for variables before.
With the assignment statement, we can assign a value for the first time, or reassign multiple times. An array name with an index can be used as we have used variable identifiers before. Both to assign and to get the stored value.

One thing we can do also is declare, create, and initialize an array all at once, as we see here in the last line, using the curly brackets.

int[] a = {2, 0 , 1 , 5 };

Note that in this case, we don’t need to write the keyword “new.”

String and array in java
If strings are ordered sequence of characters, a question we might ask is whether a string and an array of char are the same thing. They are not, although it is straightforward to convert one from the other.
Another question is whether an element of an array might also be an array.
Here, the answer is yes.


Dimensional array in java

In this way, we get what we can call bi-dimensional arrays. But even multi-dimensional arrays are also possible. This gives us interesting ways to work with arrays.

Here we see an example of an array of length 3
that holds as element arrays of integers. If all of these integer arrays are of the same length– which they not need to be– we might interpret these as matrices opening the door to interesting computations.

To conclude,we can work with ordered sequences of elements of the same type, and we call them arrays. Things to remember. The length is fixed when we declare an array.
Second, all the elements of an array have to be of the same type.
And the type of elements might be arrays as well.

Unhandled error Team – with our love

Leave a Reply

Your email address will not be published. Required fields are marked *