10.01. What’s an Array

What’s an Array

  • Eclipse: Oxygen
  • Java: 1.8

An array is basically a collection, an arrangement of similar datatypes entities. It is used to assign a common variable name for accessing a large number of entities of the same datatype by the difference of the index number. Data stored in an array has contiguous memory locations.

An array is a static data structure to store more than one value, it acts as a container that contains a collection of homogeneous elements. Thus, this tutorial deals with the initialization, working of an array, and various other datatypes. In arrays, all elements are stored under one single variable name. So, we have a reference variable that will be pointing to the array. Also, how to access the array and certain elements located at various indexes is shown here.

Note: In array, we can’t store different types of data in the same array. So if we create an array for integers, the data must be in the form of integer type only.

Types of Arrays

There are various different types in which we can represent data in an array. In Java, only two types of arrays are used:

  1. Single Dimensional Array(1D array): It is a type of array where elements are stored in a continuous row. In a 1D array, the elements of the array are defined in a row and the values are stored in a linear order(one after another).
  2. Multidimensional Array: It is a type of nested arrays, where a lot of data can be stored. Multidimensional arrays don’t have limitations with respect to memory. Hence, an ideal choice to represent a large amount of data elements.

Note: In Java, we don’t have an array of arrays known as two-dimensional arrays.

Arrays are further defined in two basic data types:

  1. Numerical arrays: int, float, double.
  2. Character arrays: These arrays are those which we declare with a char data type.

Declaration of an array in Java:

Initialization of  an array:

The length of the array is determined by the number of values provided that are separated by commas. In this example, the length of intVariableArray is 10.

You can easily access and alter the elements of an array by using its numeric index. The default initial value of index of an element of an array is 0.

Output

Foreach loop

Here we deal with the for and foreach loop which is the advanced version of for loop.  The for loop can be used to iterate over the elements of the array. The foreach loop is a shorter version of the same.

This is the ordinary approach of the “for” loop. In the following example, you can see the use of index of the array as a counter.

Output

In the following example, you can see the difference between the loops. For-Each Loop helps us to reduces the code significantly, and there is no use of the index or, rather the counter in the loop.

Output

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Rakesh
Rakesh
3 years ago

HOW TO PRINT EXACTLY BY USING FOR EACH LOOP::

hi good to see you…………….
i am having 1 doubt
if we want to print like this by using for each loop..

Student name of 1:a
Student name of 2:b
Student name of 3:c