Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » What is Array in Java? Types of array in Java

  • Rabindranath Tagore Biography in Hindi, Poems, Birthday
    Rabindranath Tagore Biography in Hindi, Title of Gurudev Biography
  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography
  • Ramayan : Short Story of ramayana in Hindi | Qualities of Rama
    Ramayan : Short Story of ramayana in Hindi | Qualities of Rama Spiritual
  • Chemical Properties of Matter (Element, Compound and Mixture)
    Chemical Properties of Matter examples (Element, Compound and Mixture) Science
  • Nelson Mandela Biography in Hindi | Nelson Mandela Day
    Nelson Mandela Biography in Hindi | Nelson Mandela Day Biography
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • What is Tense in Hindi (काल क्या है)?
    What is Tense in Hindi (काल क्या है)? Grammar

What is Array in Java? Types of array in Java

Posted on November 25, 2021November 25, 2021 By GeekCer Education No Comments on What is Array in Java? Types of array in Java
What is Array in Java? Types of array in Java

In Java, An array is a collection of items of the same type with a fixed number of items. It contains data items that are all the identical. In other ways we can say that it consists of homogeneous data items. The length of the array is determined when it is created.

Each item in an array is referred to as an element, and the array’s first element is stored at the 0th index, the second at the 1st index, and so on. The index of an array element can be used to access it.

Before going through arrays you can learn the basics of Java programming by clicking here.

Table of Contents

  • What are the limitations of array in Java?
  • How to declare arrays in Java?
  • Types of Array in Java
    • Single Dimensional Array or 1D Array
      • Array declaration and instantiation in Java
      • Example program of a single dimensional array or 1D array
    • Multi Dimensional 2D Array
      • Example program of a multi dimensional array or 2D array
    • Multi dimensional 3D array in Java
  • How to Find Array Length in Java
  • Looping through an array with for-each loop
    • Example program using for-each loop
  • Jagged Array In Java
    • Jagged array that stores two single dimensional arrays
    • Jagged array that stores a two dimensional array
    • Program to create a jagged array with single dimensional array

What are the limitations of array in Java?

There is no way to expand or reduce the size of an array once it has been established, depending on your needs. As a result, at the memory level, the array idea is not suggested.

How to declare arrays in Java?

An array is defined with square brackets.


int[] array

Types of Array in Java

  • Single Dimensional Array or 1D Array
  • Multi Dimensional Array (2D Array and 3D Array)

Single Dimensional Array or 1D Array

Syntax to declare single dimensional or 1D array


int[] array; // Syntax 1
int array[]; // Syntax 2
int []array; // Syntax 3

Array declaration and instantiation in Java


int[] array = new int[5];

Example program of a single dimensional array or 1D array


public class ArrayExample {  
  public static void main(String args[] ){  
    int[] array = new int[5];
    array[0] = 10;
    array[1] = 20;  
    array[2] = 30;  
    array[3] = 40;  
    array[4] = 50;  
    //Iterating array  
    for(int i = 0; i < array.length; i++) {
      System.out.println(array[i]);  
    }
  }
}  

Output:
10
20
30
40
50

Multi Dimensional 2D Array

Syntax to declare a multi dimensional or 2D array


int[][] array; // Syntax 1
int [][]array; // Syntax 2
int array[][]; // Syntax 3
int[] array[]; // Syntax 4
int[] []array; // Syntax 5
int []array[]; // Syntax 6	

Example program of a multi dimensional array or 2D array


public class ArrayExample {  
  public static void main(String args[] ){  
    int[][] array = new int[2][2];
    array[0][0] = 10;
    array[0][1] = 20;  
    array[1][0] = 30;  
    array[1][1] = 40;  
    //Iterating array  
    for(int i = 0; i < array.length; i++) {
      for(int j = 0; j < array[i].length; j++) {
        System.out.print(array[i][j] + " ");  
      }
      System.out.println();  
    }
  }
}  

Output:
10  20
30  40

Multi dimensional 3D array in Java

Syntax to declare a multi dimensional or 3D array


int[][][] array; // Syntax 1
int array[][][]; // Syntax 2
int [][][]array; // Syntax 3
int[] [][]array; // Syntax 4
int[] array[][]; // Syntax 5
int[] []array[]; // Syntax 6	
int[][] []array; // Syntax 7
int[][] array[]; // Syntax 8
int [][]array[]; // Syntax 9
int []array[][]; // Syntax 10

How to Find Array Length in Java

The length property is used to determine how many elements are in an array.


int[] array = {10, 20, 30, 40, 50};
int len = array.length;  // Output : 5

Looping through an array with for-each loop


for (data-type variable : array-name) {
  ...
}

Example program using for-each loop


public class ArrayExample {  
  public static void main(String args[] ){  
    int[] array = new int[5];
    array[0] = 10;
    array[1] = 20;  
    array[2] = 30;  
    array[3] = 40;  
    array[4] = 50;  
    //Iterating array  
    for (int a : array) {
      System.out.println(a);  
    }
  }
}  

Output:
10
20
30
40
50

Jagged Array In Java

Jagged arrays are arrays that contain a set of arrays within an array of that type. You may make an array in Java and use other arrays as its elements.

A single-dimensional array or a multi-dimensional array can be stored as a jagged array. Any size array may be stored in a jagged array.

What is an irregular multidimensional array? Jagged arrays are also known as irregular multi-dimensional arrays.

When dealing with a large number of arrays of varying sizes, a jagged array comes in useful.

Jagged array that stores two single dimensional arrays

              int arr[][] = new int[2][];
              

Jagged array that stores a two dimensional array


int arr[][][] = new int[3][][];            
              

Program to create a jagged array with single dimensional array


public class Example {
  public static void main(String[] args) {
    int arr[][] = new int[2][]; // Jagged Array
	   
    arr[0] = new int[2];
    arr[1] = new int[4];
	   
    arr[0][0] = 10;
    arr[0][1] = 20;
	   
    arr[1][0] = 11;
    arr[1][1] = 12;
    arr[1][2] = 13;
    arr[1][3] = 14;
	   
    System.out.println("Display First Array");
    for (int i = 0; i < 2; i++) {
      System.out.print(arr[0][i] + " ");
    }
    System.out.println();
    System.out.println("Display Second Array");
    for (int i = 0; i < 4; i++) {
      System.out.print(arr[1][i] + " ");
    }
  }
}

Output:
Display First Array
10 20
Display Second Array
11 12 13 14

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:Array in Java example, Array Length in Java, How do you count an array in Java?, How do you define an array?, How to declare array in java?, What are arrays used for Java?, What are different types of arrays?, What is array syntax?

Post navigation

Previous Post: Loop in Java, Looping statements in java (for, while, do..while)
Next Post: Method Overloading in Java

More Related Articles

Difference between JPanel and JFrame Difference between JPanel and JFrame Differences
Variables in Java Variables in Java Java
Multithreading in java Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
Difference between Comparable and Comparator Difference between Comparable and Comparator Differences
Iterate Map in Java Iterate Map in Java, Map.entrySet(), Iterators, foreach Loop Java
Internal Working of HashMap in Java | Rehashing , Hash Collision Internal Working of HashMap in Java, Rehashing, Collision Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved