HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode

Array in Java

An array is a container object that holds a fixed number of values of a single type in a contiguous memory location. It is a data structure which is used to store finite number of elements and all elements must be of similar data type.

Arrays are index based data structure so they allow random access to elements, they store. Indices start with '0'.

1. Array representation in memory

In this example, we have create an array of 5 elements. Indexes will range from '0' to '4'.

int[] a = new int[5];

a[0] = 1;
a[1] = 2;
a[2] = 4;
a[3] = 8;
a[4] = 16;

A pictorial representation of above example can be as below.

Array in memory
Array in memory

2. Array Features

  • Arrays are also a subtype of Object in Java.
  • Arrays are objects so we can find the length of the array using attribute 'length'.
  • Java array are types. we can declare the variables of array type.
  • Arrays are ordered and each have an index beginning from '0' for the first element.
  • Arrays can store primitives as well as objects. But all must be of a single type in one array instance.
  • Just like other variables, arrays can also be static, final or used as method arguments.
  • The size of an array must be specified by an int value.
  • Java arrays are Cloneable and Serializable.

3. Types of Array in Java

An array can be one of two types.

3.1. Single Dimensional Array

An array which store only primitives or objects is called single dimensional array. The general form of a one-dimensional array declaration is:

type var-name[];
OR
type[] var-name;

//Examples

int[] numbers;

String names[];

3.2. Multi-dimensional Array

A multi-dimensional array stores other arrays. It is array of arrays. In multi-dimensional array, each element of the array holding the reference of other array. A multidimensional array is created by appending one set of square brackets ([ ]) per dimension.

type var-name[][];
OR
type[][] var-name;

//Examples

int[][] cordinates;

String nameSets[][];

4. Array Examples

How to check if array contains an element
Java array clone example
Java array deep copy example
Convert string to string array
How to print an array
Java copy array range
How to copy an array
Convert byte array to string
Convert string to byte array
Print the content of an array
Remove duplicate elements in Array

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
Comments are closed on this article!

Search Tutorials

Java Array

  • Java – Array Introduction
  • Java – Print Array
  • Java – Print 2D Array
  • Java – Copy Array
  • Java – Copy Array Range
  • Java – Clone Array
  • Java – Array Deep Copy
  • Java – String to String[]
  • Java – byte[] to String
  • Java – String to byte[]
  • Java – Array Union
  • Java – Array Intersection
  • Array – Remove duplicate elements

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)