Array in java

Array is variable which is a collection of variables of same data types. You can assume arrays as blocks placed in a row in which you can fill your data with a restriction that all data must be of same type. We can define an array in three ways:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // syntax of array
  7. // array of integers
  8. // first method of initialisation
  9. int array1[] = {2,3,4};
  10. // second method
  11. // define array
  12. int[] array2 = new int[3];;
  13. // above line is equivalent to int array2[];
  14. // collection of 3 variables but still all values are uninitialised
  15. // initialise values
  16. array2[0] = 2;
  17. array2[1] = 3;
  18. array2[2] = 4;
  19. // third method
  20. int array3[];
  21. // above array (array2) is null or uninitialised
  22. array3 = new int[3];
  23. // initialise values
  24. array3[0] = 2;
  25. array3[1] = 3;
  26. array3[2] = 4;
  27. }
  28. }

Note these points:

  1. array1 is collection of three integer variables => array1[0], array[1] and array[2]. We access these variables using there indexes (positions, starting from 0). We have defined all three arrays (array1, array2 and array3 ) as integer but arrays can be any data type like char, double, object etc.
  2. Like variables we can define array at one place and initialize it at another place (like array2 and array3). It is useful property because in practical programs we use mostly this type of initialisations.

Array with loop:

We access array with loop like this:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // Initialise array
  7. double list[] = {2.13 ,3 ,4.4 ,64.54 ,75};
  8. // for loop
  9. // length is property of array and returns value of
  10. // number of elements in the array, here 5
  11. for(int i = 0;i< list.length;i++)
  12. {
  13. // here we have each element of array one by one
  14. System.out.println(list[i]);
  15. }
  16. }
  17. }

Foreach loop in java:

Now we can use foreach loop to access each element of array inside the loop one by one. In foreach loop, we get next element of the array inside the loop. This example is equivalent to above example:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // Initialise array
  7. double list[] = {2.13,3,4.4,64.54,75};
  8. // for loop
  9. for(double temp : list)
  10. {
  11. // here we have each element of array one by one
  12. // we are assigning next element of list to temp in each iteration
  13. // starting from first element
  14. System.out.println(temp);
  15. }
  16. }
  17. }

Note that data type of temporary variable ( temp ) must be same as iterative variable ( list ).

Multidimensional Array:

Notice that an array is a variable and it contains variables. So what will happen if we make an array of type array? It will be a multidimensional array. For a two dimensional array you may assume it as a table of m*n. For example, I am storing following data in a two dimensional array:

121728
832398
98332
909231

The code will be:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. /*
  7. * table is :
  8. * 12, 17, 28
  9. * 83, 23, 98
  10. * 9, 83, 32
  11. * 90, 92, 31
  12. */
  13. // it is (array[])[] i.e. array of arrays
  14. // equivalent to int[][] multiarray
  15. int multiarray[][] = {
  16. // each row is a new array
  17. {12, 17, 28},
  18. {83,23,98},
  19. {9,83,32},
  20. {90,92,31}
  21. };
  22. // for loop for multidimensional array
  23. // multiarray is our first array
  24. for(int i = 0;i < multiarray.length;i++)
  25. {
  26. /*
  27. * inside multiarray we have 4 arrays
  28. * multiarray[0] to multiarray[3]
  29. * i is also varying from 0 to 3
  30. * so our inner arrays are multiarray[i]
  31. */
  32. // in other words we have row of table as multiarray[i]
  33. for(int j = 0; j < multiarray[i].length;j++)
  34. {
  35. /*
  36. * here we have multiarray[i][j]
  37. * if we show in table then we have
  38. * ith row and jth column of the table
  39. */
  40. // we are printing table
  41. // this will print one row for each inner loop
  42. // or say for each value of i, j will vary from 0 to lenght of row
  43. System.out.print(multiarray[i][j] + " ");
  44. }
  45. //after each row we want to change the line
  46. System.out.println();
  47. }
  48. }
  49. }

This example is lengthy and some complex also. If you could not understand this example then don't worry. I am describing it again.

  • In line 16, we are defining a multidimensional array. It is defined by placing one more square braces after variable miltiarray. If we place one more square braces after it then ir will be 3-dimensional. We are pacing elements (values) of each row in a new array. So we have 4 inner arrays, each corresponding to each row. Elements of each array are columns.
  • In line 26, we are writing a loop for outer array (multiarray). This will navigate to its each element (each row) which are arrays.
  • In line 36, we are using one more loop. In this loop, we have one array (row) each time say multiarray[i] (ith row). After inside this loop we have one by one each element (column) of that array (row). We are printing that element (column) in line 47.
  • Line 51 is outside the inner loop but still inside the outer for loop. This line will be executed after printing each array (row) and will print a new line (will change line).
  • By this way we are printing a table. If we have to operate a 3-dimensional array then we will require 3 loops.

In above example, variable can define and initialize by second and third methods (defined in first example). Multidimensional arrays can also be operated using foreach loops. We can write above example like this:

  1. package findalltogether;
  2. public class Tutorials
  3. {
  4. public static void main(String args[])
  5. {
  6. // 4 rows and 3 columns
  7. // in this case all columns must be of
  8. // equal lenght say 3
  9. int multiarray[][] = new int[4][3];
  10. // each row is a new array
  11. multiarray[0][0] = 12;
  12. multiarray[0][1] = 17;
  13. multiarray[0][2] = 28;
  14. multiarray[1][0] = 83;
  15. multiarray[1][1] = 23;
  16. multiarray[1][2] = 98;
  17. multiarray[2][0] = 90;
  18. multiarray[2][1] = 92;
  19. multiarray[2][2] = 31;
  20. // for loop for multidimensional array
  21. // multiarray is our first array
  22. for(int[] outside: multiarray)
  23. {
  24. // each time a new row will be assign to outside
  25. for(int inside : outside)
  26. {
  27. // each column will be assing to inside each time
  28. System.out.print(inside + " ");
  29. }
  30. //after each row we want to change the line
  31. System.out.println();
  32. }
  33. }
  34. }


About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.