Thearray is one of the fundamental and most useful concepts in programming. Most programming languages let programmers create and use arrays. Java is one of them.
An array is a group of variables that have the same name and data type. To access one of the variables, use the array name and the integer index of the variable. In Java, array indexes start with0
. Arrays have a fixed length. Java implements arrays as objects.
To declare an array namedarr
that can hold integer values:
int[]arr;
In the code above,arr
is the variable name.int[]
is the type, which is an array of integer values. This code only declares thearr
variable. Here is how to allocate an array object with5
integer elements:
int[]arr=newint[5];
In this code,new
is a Java keyword that allocates memory for an object andint[5]
specifies an array of5
integer values. Since the default value for Javaint
variables is0
, the array contains five0
values in positions0
-4
.
Here is another way to allocate the same array:
int[]arr=newint[]{0,0,0,0,0};
The array object has alength
property to indicate the size of the array:
intlen=arr.length;
In this array example,arr.length
would return5
.
To access an individual element of an array, supply an index to the array variable. The index is in square brackets[ ]
after the array name:
arr[0]= 100;arr[1]= 200;arr[2]= 300;arr[3]= 400;arr[4]= 500;System.out.println( arr[3]); // 400
The first five lines assign the values100
,200
,300
,400
, and500
to array positions0
,1
,2
,3
, and4
respectively. Note that array elements are like integer variables, except with integer indexes.
The last line outputs the array element at position3
:400
. As you can see, an array element can be wherever an integer value is allowed.
Since the array indexes form a sequence of integers, arrays work well with loops. For an array of length5
, it has the indexes0
-4
. Here is an example that uses afor
loop to output each element of our array:
for(inti=0;i<arr.length;i++){System.out.println(arr[i]);}
And here is the output:
100200300400500
This example uses the loop variablei
as the index of the array. The loop repeats its statement five times, once for each index of the array.
What if we want to sum all the elements in the array? It's easy. Use a loop:
intsum=0;for(inti=0;i<arr.length;i++){sum=sum+arr[i];}System.out.println(sum);// 1500
Here is the complete program:
publicclassExample{publicstaticvoidmain(String[]args)throwsException{int[]arr=newint[]{0,0,0,0,0};arr[0]=100;arr[1]=200;arr[2]=300;arr[3]=400;arr[4]=500;intsum=0;for(inti=0;i<arr.length;i++){sum=sum+arr[i];}System.out.println(sum);// 1500}}
Arrays are very useful in programming. Think of an array like a group of variables, all with the same name and data type. Use an index to access each element. You can assign a value to an element of the array or access the value of an element, just like plain variables. Use arrays with loops to see the true power of arrays.
Thanks for reading.
Follow me on Twitter@realEdwinTorres
for more programming tips. 😀
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse