0% found this document useful (0 votes)
33 views7 pages

Unit 4 Arrays

Uploaded by

ar2477897
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)
33 views7 pages

Unit 4 Arrays

Uploaded by

ar2477897
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
You are on page 1/ 7

Array

Subject: Object Oriented Programming Language using JAVA

Faculty name: Bhushan Shewale.


Array: Array is an continuous block of memory where we can store the homogenous type of
data or same type of data.

In order to store same type of value ,we need to create an array object [block of
memory]. Before we creating an object we need to create a block of memory to store the
address of the array object.

To store the homogenous type of values using array we need to create Array
reference variable and Array Object. To make use of array reference variable we can store
the address of Array object. We can create the array for both Primitive and Non – Primitive
Data type.
Characteristics of Array:
1. Array is a continuous block of memory which is used to store multiple value or multiple
data.
2. The array size must be specified at the time of Declaration
3. Once the array size is specified those many continuous blocks will created, once the
blocks will created later we can not increase or decrease the size of an array.
4. Hence array is a fixed length of memory.
5. Based on index values we can store the data in that block when the size of block will
specified.
6. Array block consist of index values.
7. The index of an array is start from zero to ends at array length-1.
8. Hence we can access the element or initialize the element into an array by using indexing
value.
Note: Every Array is an object in Java.
1. Declaring Array Variables
To use an array in a program, you must declare a variable to reference the array and
specify the array’s element type. Here is the syntax for declaring an array variable.
Syntax: data_type[ ] refVariable_name;
data_type refVariable_name[ ];
The element type can be any data type, and all elements in the array will have the
same data type. for example following conditions declared the variable ' a ' that reference an
array of different data type.

Example:

1. Creating single dimensional array reference variable of: int a = int[ ] a;

2. Creating single dimensional array reference variable of: String b = String[ ] b;

3. Creating single dimensional array reference variable of: double c = double[ ] c;


• Instantiation an Array:
Unlike declarations for primitive data type variables, the declaration of an array
variable does not allocate any space in memory for the array. It creates only a storage
location for the reference to an array. If a variable does not contain a reference to an array, the
value of the variable is null. You cannot assign elements to an array unless it has already been
created. After an array variable is declared, you can create an array by using the new operator
and assign its reference to the variable with the following syntax:
Syntax: refVariable = new data type [ size ];
Example:
Five blocks
1 2 3 4 5 created inside
Heap Area.
0th 1th 2th 3th 4th
Five blocks
created inside
Null Null Null Null Null
Heap Area.
0th 1th 2th 3th 4th
1D Array
In Java, a 1D array is a linear data structure that consists of a collection of elements of
the same data type, arranged in a single row. Each element in a 1D array is accessed using its
index, which starts from 0 and goes up to the array's length minus one.

2D Array
In Java, a 2D array is an array of arrays, also known as a matrix. It consists of rows and
columns, forming a grid-like structure. Each element in a 2D array is accessed using two
indices: one for the row and another for the column.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy