In programming, an array is a collection of elements of the same type.

An Array is a group of like-type variables that are referred to by a common name.

A variable is not consistent or having a fixed pattern; liable to change.

In computer science, an array data structure, or simply an array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key. They are also used to implement many other data structures, such as lists and strings.

The array is an abstract data type (ADT) that holds a collection of elements accessible by an index. The elements stored in an array can be anything from primitives types such as integers to more complex types like instances of classes. ... The array (ADT) is usually implemented by anArray (Data Structure).

In programming, a variable is a value that can change, depending on conditions or on information passed to the program.

Following are some important point about Java arrays.

  • In Java all arrays are dynamically allocated.(discussed below)

  • Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.

  • A Java array variable can also be declared like other variables with [] after the data type. Ex: String [], int [], float [], char [].

  • The variables in the array are ordered and each have an index beginning from 0.

  • Java array can be also be used as a static field, a local variable or a method parameter.

  • The size of an array must be specified by an int value and not long or short.

  • The direct superclass of an array type is Object.

  • Every array type implements the interfaces Cloneable and

Array can contains primitives (int, char, etc) as well as object (or non-primitives) references of a class depending on the definition of array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class,the actual objects are stored in heap segment.

One-dimensional array:

Accessing Java Array Elements using for Loop

public class Array{

public static void main(String [] args){
 int [] intArray;
 intArray = new int[]{1,2,3,4,5};
 //if you don't know all Array elements, just say int[5] to declare 5 elements

 for (int i = 0i < intArray.lengthi++){
 System.out.println("Element at index " + i + 
 " : "+ intArray[i]);

Although the above first declaration establishes the fact that intArray is an array variable, no array actually exists. It simply tells to the compiler that this(intArray) variable will hold an array of the integer type. To link intArray with an actual, physical array of integers, you must allocate one using new and assign it to intArray.

Instantiating an Array in Java

When an array is declared, only a reference of array is created. To actually create or give memory to array, you create an array like this:The general form of new as it applies to one-dimensional arrays appears as follows:

  1. The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types).Refer Default array values in Java

  2. Obtaining an array is a two-step process. First, you must declare a variable of the desired array type. Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. Thus, in Java all arrays are dynamically allocated.

Two-dimensional array:

Multidimensional Arrays

Multidimensional arrays are arrays of arrays with each element of the array holding the reference of other array. These are also known as Jagged Arrays. A multidimensional array is created by appending one set of square brackets ([]) per dimension. Examples:

int[][] intArray = new int[10][20]; //a 2D array or matrix
int[][][] intArray = new int[10][20][10]; //a 3D array

filter_noneedit play_arrow brightness_4 class multiDimensional { public static void main(String args[]) { // declaring and initializing 2D array int arr[][] = { {2,7,9},{3,6,1},{7,4,2} }; // printing 2D array for (int i=0; i< 3 ; i++) { for (int j=0; j < 3 ; j++) System.out.print(arr[i][j] + " "); System.out.println(); } } }


2 7 9 
3 6 1 
7 4 2 

Arrays of Objects

An array of objects is created just like an array of primitive type data items in the following way.

 Student[] arr = new Student[7]; //student is a user-defined class

The studentArray contains seven memory spaces each of size of student class in which the address of seven Student objects can be stored.The Student objects have to be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.

Student[] arr = new Student[5];

// Java program to illustrate creating an array of 
// objects 
class Student 
 public int roll_no; 
 public String name; 
 Student(int roll_no, String name) { 
 this.roll_no = roll_no; = name; 
// Elements of array are objects of a class Student. 
public class GFG { 
 public static void main (String[] args) { 
 // declares an Array of integers. 
 Student[] arr; 
 // allocating memory for 5 objects of type Student. 
 arr = new Student[5]; 
 // initialize the first elements of the array 
 arr[0] = new Student(1,"aman"); 
 // initialize the second elements of the array 
 arr[1] = new Student(2,"vaibhav"); 
 // so on... 
 arr[2] = new Student(3,"shikar"); 
 arr[3] = new Student(4,"dharmesh"); 
 arr[4] = new Student(5,"mohit"); 
 // accessing the elements of the specified array 
 for (int i = 0; i < arr.length; i++) 
 System.out.println("Element at " + i + " : " + 
 arr[i].roll_no +" "+ arr[i].name); 


Element at 0 : 1 aman
Element at 1 : 2 vaibhav
Element at 2 : 3 shikar
Element at 3 : 4 dharmesh
Element at 4 : 5 mohit


Arrays are sequences. Python has 3 main sequence data structure classes:

List [1,2,3]

Tuple (1,2,3)

String '123'

All supporting indexing at 0.

Mutable vs Immutable Objects in Python: Simple put, a mutable object can be changed after it is created, and an immutable object can't. Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. Objects of built-in types like (list, set, dict) are mutable.

A data structure is a way to organize and store data. Technically a data structure is an object, but it's an object with the specific use for holding other objects.

clarifies about the differences between objects and data structures:

  • Objects hide their data behind abstractions and expose functions that operate on that data. Data structures expose their data and have no meaningful functions.

  • Object expose behavior and hide data. This makes it easy to add new kinds of objects without changing existing behaviors. It also makes it hard to add new behaviors to existing objects.

  • Data structures expose data and have no significant behavior. This makes it easy to add new behaviors to existing data structures but makes it hard to add new data structures to existing functions.

Python Lists Vs Arrays

We can treat lists as arrays. However, we cannot constrain the type of elements stored in a list. For example:

a = [1, 3.5, "Hello"] 

If you create arrays using the array module, all elements of the array must be of the same numeric type.

import array as arr
# Error
a = arr.array('d', [1, 3.5, "Hello"])


Traceback (most recent call last):
  File "<string>", line 3, in <module>
    a = arr.array('d', [1, 3.5, "Hello"])
TypeError: must be real number, not str

Low-level Architecture of Arrays

Memory of computer stored in bits.

Typical unit is bytes, containing 8 bits.

Computers use a memory address.

Each byte associated with a unique address.

ex; Byte #2144 vs Byte #3134

Representation of computer memory:

Individual bytes with consecutive addresses

computer hardware is designed so that any byte of the main memory can be efficiently accessed based upon its memory address

computer's main memory performs as a random access memory.

just as easy to retrieve let's say byte number 8 6 7 5 3 0 9 as it is to retrieve byte number 309

each individual byte of memory can be stored or retrieved in O(1) time.

So in constant time

programming language keeps track of the association between an

identifier and the memory address in which the associated value is stored.

A group of related variables can be stored one after another in a contiguous portion of the computer's memory and we can then note that representation as an array.

Example: text string stored as an ordered sequence of individual characters

Python internally represents each Unicode character with 16 bits or two bytes for each character.

Dynamic Arrays, Dynamic Memory (allocation), and C++ Pointers and Python


Recent Posts

See All

AI behavioral prediction

what you want is ultimately to learn about human behaviors and anticipate them. That’s called behavioral prediction. These behavioral predictions are made using recurrent neural networks: they use pas