java tutorial - Arrays in Java Example - java programming - learn java - java basics - java for beginners



Java Arrays:

 Arrays in Java

Learn java - java tutorial - Arrays in Java - java examples - java programs

  • The array is an object which contains elements of similar data type. It is a data structure where we can store similar elements. We store only fixed set of elements in a java array.
  • Array in java is index based; first element of the array is stored at 0 indexes. It is a group of like-typed variables which are referred by one common name. Arrays in Java work differently than they do in C/C++. Following are some important point about Java.
  • An array is an ordered list of values
  • Array of size N is indexed starts from 0 to N-1.
  • A specific value in an array is referenced using the array name and followed by the index in brackets.

Types of Array in java:

There are two types of array.

  1. Single Dimensional Array
  2. Multidimensional Array

Single Dimensional Array in java

Syntax

	dataType[] arr; (or)  
	dataType []arr; (or)  
	dataType arr[];  

Multidimensional array in java:

dataType[][] arrayRefVar; (or)  
dataType [][]arrayRefVar; (or)  
dataType arrayRefVar[][]; (or)  
dataType []arrayRefVar[];   

Sample Code:

public class  kaashiv_infotech
{
    public static void main(String[] args)
    {
        int[] arr = {11,23,56,24,15};
        int count=0;
        while(count < 5)
        {
        System.out.println(arr[count]);
        count++;
        }
    }
}

Output:

11
23
56
24
15

Advantage of Java Array:

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.

Disadvantage of Java Array:

  • Limited size: We will store only a fixed size of elements in an array. It doesn't grow its size at the runtime. To solve this problem, the collection framework is used in java.

Related Searches to Arrays in Java Example