Java Array | Java foreach LoopA 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.
The syntax of Java foreach loop is,
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.
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
7.2 Creation And Declaration Of Array In Java
7.3 Arraylist Access Using Index
7.4 Java Multidimensional Array
7.5 Java Array Initialization
7.6 Learn Arrays And Loops
7.7 Java Code To Print Student Details Using Arrays
7.8 For-each Loop In Core Java Programming
7.9 Command Line Arguments In Core Java Programming
Dependent Topics : 6. Methods |