Menu
Topics Index
...
`

Java Array | Java foreach Loop

A Java array is a container object which holds specific number of values of same type. In other words, a Java array is a data structure that stores a specified number of elements of single type. When the creation of an array, we specify the length of it. After specifying, it is fixed. Each element in a Java array is called an element and every element can be accessed by its numeric index.

A Java array is useful for storing a data collection, but it is more often useful to think of an array as a variable collections of single type. In Java array, we declare an array variable such as strings and using strings[0], strings[1], strings[2],..... to representing individual variables.

In Java array, its declaration contains two components. They are, array type and array name. Java array type is represented as type[ ]. Here, type is the elements data type; the brackets indicate that the variable holds an array. The size of Java array is not related to its type. We can give any name for Java arrays according to our convenience. Actually, the declaration can't create an array; it just informs the compiler that the variable can hold a array of specified type.

In arrays, Java foreach loop plays a vital role; because it is a special type of looping structure which is useful for specific looping operations in arrays. Java foreach loop was introduced in Java5. It is mostly used for traversing an array or collection of elements or simply, Java foreach loop is very useful for iterations over arrays and such other collections. The Java foreach loop contains the following advantages.

  • Java foreach loop makes the coding more readable.
  • Java foreach loop avoids the possibility of programming bugs.

The syntax of Java foreach loop is,

  • for(data_type variable : array / collection)
    {
          //statements
    }

Even though we use for loop for iteration operations, the foreach loop makes the code more readable and simple manner when deals with big number. Java foreach loop is better replacement for the for loop, but not every time; especially, when we encountered the following conditions.

  • Assigning the elements: When we assigning values to the elements, we must avoid usage of Java foreach loop. When desired access is needed then only we use this loop.
  • Single structure usage: We can't use the Java foreach loop when we comparing two arrays in a situation.
  • Only forward iterations: For forward looping only, use the Java foreach loop and that too in one step.
  • Compatibility: If we want our coding to be compatible with different versions previous to Java5, you might prefer the regular for loop.

Java also supports multi-dimensional arrays such as 2-dimensional arrays, 3-dimensional arrays, etc. A 2 dimensional array consists of many 1-dimensional arrays. So, these arrays are also called as arrays of arrays. 2-dimensional arrays can be same as a matrix or table. The first dimension is for rows and second dimension is for columns.

Apart from these, this chapter deeply include the concepts Java Array, Creation And Declaration Of Array, Arraylist Access Using Index, Multidimensional Arrays, Array Initialization, Arrays And Loops, foreach loop, Command Line Arguments and example programs which explained below.


In this section, we'll discuss in detail about:
7.1 Java Array
  • Arrays are used to store lots of similar data in one variable instead of multiple variables. That is, if we want to store the scores (or runs) of various cricket teams, we can either...Read More

7.2 Creation And Declaration Of Array In Java
  • Unlike primitive data types, any array has to be created using the new operator. A reference to an array can be declared without any elements and can be assigned to ...Read More

7.3 Arraylist Access Using Index
  • Since array contains multiple elements, but we have only one variable, we use index to access the elements. The index is nothing but an unique number which identifies each ...Read More

7.4 Java Multidimensional Array
  • Java supports multi dimensional arrays like 2 dimensions, 3 dimensions etc., A 2-dimensional array (2D array) is comprised of many one-dimensional (1D) arrays . They are also ...Read More

7.5 Java Array Initialization
  • Array Initializer provides a way to initialize the array elements with values, the moment it is declared. This removes the two step process of declaring and then initializing the ...Read More

7.6 Learn Arrays And Loops
  • Arrays are very useful in reducing the number of variables created and in reducing the code complexity. This is because the size of the array can be initialized dynamically and...Read More

7.7 Java Code To Print Student Details Using Arrays
  • The usability of the arrays is further enhanced when we use loops, it is very efficient to write programs on arrays. The program shows how to print details of multiple students...Read More

7.8 For-each Loop In Core Java Programming
  • The for-each loop or enhanced for loop introduced in JDK 1.5 is very useful in iterating through the elements of the array. This for loop simplifies the iteration of elements in ...Read More

7.9 Command Line Arguments In Core Java Programming
  • Java supports command line arguments, which means you can pass parameters/arguments to the program. Depending upon the parameters passed to the program...Read More

Dependent Topics : 6. Methods 

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App