0% found this document useful (0 votes)
3 views5 pages

Array

The document provides an introduction to arrays and ArrayLists in Java, explaining their purpose, syntax, and internal workings. It covers one-dimensional and two-dimensional arrays, including declaration, initialization, and memory allocation. Additionally, it includes example programs for inputting and printing array elements using the Scanner class.

Uploaded by

singhchs91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Array

The document provides an introduction to arrays and ArrayLists in Java, explaining their purpose, syntax, and internal workings. It covers one-dimensional and two-dimensional arrays, including declaration, initialization, and memory allocation. Additionally, it includes example programs for inputting and printing array elements using the Scanner class.

Uploaded by

singhchs91
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Arrays & ArrayList in Java

Why do we need Arrays?


It was simple when we had to store just five integer numbers and now let’s
assume we have to store 5000 integer numbers. Is it possible to use 5000 variables?
NO
To handle these situations, in almost all programming languages we have a concept
called Array.
Array is a data structure used to store a collection of data.

Syntax of an Array:
datatype[] variable_name = new datatype[size];

For example, we want to store roll numbers:


int[] rollnos = new int[5];

OR
int[] rollnos = {51, 82, 13, 15, 16};

int[] represents the type of data stored in array.


All the type of data in array should be same!

Internal working of array:


int[] rollnos; // declaration of array

rollnos are getting defined in stack


rollnos = new int[5]; // initialization

Here, object is being created in heap memory. Actual memory allocation happens here.

Declaration vs Initialization
Declaration happens at compile time
int[] arr = new int[5];

Initialization happens at run time (creating object in heap memory)


This above concept is known as Dynamic memory allocation, which means at runtime
or execution time memory is allocated.

Internal Representation of Array:


Internally in Java, memory allocation totally depends on JVM whether it be continuous
or not!
Reason 1: Objects are stored in heap memory.
Reason 2: In JLS (Java Language Specification) it is mentioned that heap objects are
not continuous.
Reason 3: Dynamic memory allocation. Hence, array objects in Java may not be
continuous (depends on JVM).

Index of an array:
Index represents the position of elements in an array. Array indexing starts from 0.
Example array: [3, 8, 9, 10, 53, 33]
Indices: 0 1 2 3 4 5
arr[0] = 3 arr[1] = 8 arr[2] = 9 arr[3] = 10 arr[4] = 53 arr[5] = 33

Suppose we change the value of certain index:


arr[4] = 99;

New array will be: [3, 8, 9, 10, 99, 33]

new keyword:
int[] arr = new int[5];

It will create an object in heap memory of array size 5.


If we don’t provide values in the array, internally by default it stores [0, 0, 0, 0,
0] for above size of array.
String[] arr = new String[4];

Itself is an object and will be stored in different parts of heap memory.

Important Notes:
• Primitives (int, char etc.) are stored in stack.
• All other objects are stored in heap memory.
• In an array, since we can change the objects, hence they are mutable.
• Strings are immutable.
• [Link](array) internally uses for loop and gives the output in proper format.
Example:
public class OneDArrayExample {
public static void main(String[] args) {

// Declaration and Initialization


int[] numbers = {10, 20, 30, 40, 50};

// Accessing elements
[Link]("First element: " + numbers[0]);

// Printing all elements using loop


[Link]("All elements:");
for (int i = 0; i < [Link]; i++) {
[Link](numbers[i]);
}
}
}

2D Array:
A 2D array is an array of arrays, represented as rows and columns.
int[][] arr = new int[size][];

OR
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

Note: not mandatory to give size of rows.


In a 2D array, internally each row is stored as a separate 1D array in heap memory. The
2D array reference in stack points to these row arrays in the heap.

Example:
public class TwoDArrayExample {
public static void main(String[] args) {

// Declaration and Initialization


int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};

// Printing matrix using nested loop


[Link]("Matrix elements:");

for (int i = 0; i < [Link]; i++) {


for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
One-Dimensional Array Input Using Scanner
Example Program

import [Link];

public class ArrayInputExample {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter size of array: ");


int n = [Link]();

int[] arr = new int[n];

[Link]("Enter " + n + " elements:");

// Taking input
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

// Printing array
[Link]("Array elements are:");
for (int i = 0; i < n; i++) {
[Link](arr[i]);
}

[Link]();
}
}
Two-Dimensional Array Input Using Scanner
Example Program

import [Link];

public class TwoDArrayInput {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number of rows: ");


int rows = [Link]();

[Link]("Enter number of columns: ");


int cols = [Link]();

int[][] matrix = new int[rows][cols];

[Link]("Enter matrix elements:");

// Taking input
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}

// Printing matrix
[Link]("Matrix is:");

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}

[Link]();
}
}

You might also like