CodeNewbie Community

Cover image for Introduction To Arrays
Abhann
Abhann

Posted on

Introduction To Arrays

In this section, we will get introduced to arrays. I like to consider this chapter a big step into the world of programming. Understanding this section will definitely validate your love for coding, so I want to take some time to explain arrays and its concept.
Now, suppose I want to represent one “int” data type literal or value “10”, then our code would look like:
image

If I also want to represent four “int” values of {10, 20, 30 and 40} then it would look like;
image

But suppose I want to represent ten thousand values, it’s a lot right? Yes, it is and then going for ten thousand variables will be considered the worst kind of programming practice, and it follows that readability of the code is going to be stressful.
If you want to represent a large number of values by using a single variable, then you can happily go for the array’s concept.
Now, let’s represent these ten thousand values, it will be expressed as;
image
This brings us to the definition of arrays.

o What are Arrays?

An array is an indexed collection of a fixed number of homogenous data elements. The main advantage of arrays is we can represent a huge number of values by using a single variable, so that readability of the code will be improved.

Before we move on, let’s quickly discuss the biggest limitations of arrays, as well as its advantages, because it helps to create the avenue to appreciate the concepts of Collections, collections concept helps where arrays can’t, but don’t think about collections yet, let’s come back to arrays, alright, below are arrays limitations and advantage.

• Limitations
Arrays are fixed in size(this is its biggest limitation).
Arrays can hold only homogeneous(similar) data type elements.

• Advantage
A huge number of values can be represented by using a single variable.
Once we create an array, there is no chance of increasing or decreasing the size based on our requirements, hence, to use array’s concept, it is mandatory that we know the size in advance, which may not be always possible.

o ARRAY DECLARATION:

• One dimensional array declaration, as seen below;
image

All the above are correct, but among them, the recommended one is the first.
The first one is the recommended one, because the variable name is clearly separated from the data type, also the last line is defined as a C- style array declaration of local variable.

• Two dimensional array declaration:
image
As seen above, the compiler ignores the space and the total arrays are considered accordingly, irrespective of the positioning. But I like to say good programming practice recommends we use the first line of code.
Observe the following;
image
The reason for the above is to try and understand defining variable declaration for multiple variables, you can practice and name more on your own to get the logic behind it. If you want to declare or specify a dimension before the variable, this facility is applicable only for the first variable in your declaration, if you try to apply this for the remaining variables, we will get a compile time error, as seen below;
image

• Three dimensional array declaration:
image

Before we go to Arrays creation, we will talk about the two rules governing array declaration, and they are, below;
i. In the time of an array declaration, we can’t specify its size.
ii. If we want to represent a dimension before the variable, it is fine, but this rule is only applicable for the first variable.

o ARRAY CREATION

Observe;
image
The above array is an object with base size 3, this is because every array in Java is an object, and hence we can create arrays using the “new” operator(as shown above).
Remember the section on variables, you will notice we created objects for classes and so, as seen, similarly above, we are creating array objects.

For every array type, a corresponding class should be there also, if the class isn’t there, you can’t create an object.

Hence, for every array type, corresponding classes are available, but these classes are part of the Java language level and not available at the programmer level.
Here’s proof;
image
The “[“ means – square bracket one dimensional array, and the “I” means – int type.
For a two-dimensional array, we get;
image
Recall, I have mentioned that for every array type, corresponding classes are available, the command to get this is “getClass().getName()”.
image
In conclusion to this section, I will state the various loop holes to beware of in relation to one dimensional array creation.
i. At the time of array creation, it is compulsory to specify the size.

ii. It is legal to have an array with zero “0” size.

iii. If you want to specify an array size with a negative data type value, then you will get a run time exception saying “NegativeArraySizeException”, but there will be no compile error.

iv. To specify array size, the allowed data types are byte, char, short and int.

v. The maximum allowed array size in Java is 2147483647, which is the max value of the int data type.

Discussion (0)