Java Array


Java Array:
Normally, an array is a collection of similar type of elements which have a contiguous memory location.
Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need to use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like C/C++, we can also create single dimentional or multidimentional arrays in Java.


Features of Array
1.     Arrays are objects
2.     They can even hold the reference variables of other objects
3.     They are created during runtime
4.     They are dynamic, created on the heap
5.     The Array length is fixed
Array Declaration:
dataType[] arrayName;
·        dataType can be aprimitive data type like: int, char, Double, byte etc. or an object (will be discussed in later chapters).
·        arrayName is an identifier.
Advantages
·        Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently.
·        Random access: We can get any data located at an index position.
Disadvantages
·        Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
Types of Array in java
There are two types of array.
1.     Single Dimensional Array
2.     Multidimensional Array
Single Dimensional Array:
One-dimensional array in Java programming is an array with a bunch of values having been declared with a single index.
Syntax to Declare Array in Java:
                     dataType[] arr; (or)  
                     dataType []arr; (or)  
                     dataType arr[];

For-each Loop for Java Array

We can also print the Java array using for-each loop. The Java for-each loop prints the array elements one by one. It holds an array element in a variable, then executes the body of the loop.
Multidimensional Array:
Multidimensional Arrays can be defined in simple words as array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order).
Syntax to Declare Array in Java:
                    dataType[][] arrayRefVar; (or)  
                    dataType [][]arrayRefVar; (or)  
                    dataType arrayRefVar[][]; (or)  
                    dataType []arrayRefVar[]; 

Jagged Array in Java

If we are creating odd number of columns in a 2D array, it is known as a jagged array. In other words, it is an array of arrays with different number of columns.








Comments

Post a Comment

Popular posts from this blog

Introduction to Apache Spark with Scala:

Abstract class in Java