0% found this document useful (0 votes)
2 views6 pages

PIJ Array Converted 1

The document introduces the concept of arrays in Java, explaining their purpose to store multiple values in a single variable, overcoming limitations of ordinary variables. It defines arrays as collections of similar data types and discusses their advantages, disadvantages, and types, including one-dimensional and two-dimensional arrays. Additionally, it provides syntax for declaring, creating, and initializing arrays, along with examples of input and output operations.

Uploaded by

mridulapankajroy
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)
2 views6 pages

PIJ Array Converted 1

The document introduces the concept of arrays in Java, explaining their purpose to store multiple values in a single variable, overcoming limitations of ordinary variables. It defines arrays as collections of similar data types and discusses their advantages, disadvantages, and types, including one-dimensional and two-dimensional arrays. Additionally, it provides syntax for declaring, creating, and initializing arrays, along with examples of input and output operations.

Uploaded by

mridulapankajroy
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/ 6

Programming in JAVA by Roly Sinha

ARRAY

This very new concept was introduced to overcome the


problems encountered often associated with ordinary variables
we have learned so far. So before dealing with it, we must be
known what the problems were.
To better understand the problem, let us consider an example:

class Problem
{
public static void main(String args[])
{
int x=0;
x=5;
x=10;
System.out.println(“X= ” + x);
}
}

This very example will definitely print the value of x as 10. Why
so? Because when a value 10 is assigned to x, the earlier value
of x, i.e. 5, is overlapped by new value. Thus the ordinary
variables we have used so far is only capable of holding one
value at a time. However, in some situations we would want to
store more than one value at a time in a single variable.
For example, suppose we wish to arrange the percentage marks
obtained by 100 students, we have two options to store these
marks in memory:
a. Construct 100 variables to store percentage marks
obtained by 100 different students.
b. Construct one variable (array) capable of storing or holding
all the hundred values.

1
Programming in JAVA by Roly Sinha

Now, in the above two options, of course second alternative is


better. Simple reason is that, it will be easy to handle one single
variable than handling 100 different variables.

Definition:

An array is a collection of similar data types. It is a group of


related data items that share a common name.
It is also known as

“Linear data structure”(as all elements are stored sequentially).


Or
“Non-primitive data type”(as it is defined by users).
Or
“Static data structure”(memory will allocate at compile time).
Or
“Dense list”(as it cannot grow or shrink at run time).
Or
“Homogeneous list”(as all elements must be of same type).
Or
“Subscripted variable[]”(square bracket is a notation of array).

All array elements will be accessed by same name but different


index. First element of array will be stored at 0th position and
last will be size-1.

Advantages:-
• Variables of same type can be stored together without
declaring separate variable for each.
• Accessing, deletion and insertion in array is fast and easy.

2
Programming in JAVA by Roly Sinha

Disadvantages:-
• The memory location is limited; we cannot increase or
decrease the space at run time.

Types of Array:

There are basically following types of array:-


1. One Dimensional Array--- []
2. Two Dimensional Array---[][]
3. N-Dimensional Array---[][][]..
4. Jagged Array (an extension on 2D Array)

(Jagged array is array of array such that member arrays can be


of different sizes, i.e., we can create a 2-D arrays but with
variable number of columns in each row.)

Each array element is referred to by specifying the array name


followed by one or more subscripts, with each subscript
enclosed in square brackets [].

1. One dimensional Array:


• Also called linear array/single subscripted variable.
• The indexing starts from 0 and ends at n-1 were ‘n’ is the
total no. of memory location that has been reserved by the
array.
• Denoted by: [] (single square bracket)

3
Programming in JAVA by Roly Sinha

Creation of 1D Array in Java:-


1. Declaring the array.
2. Creating memory location.
3. Putting values into the memory locations.

Declaring one-dimensional array

Form 1: datatype array_name[];


Form 2: datatype[] array_name;
Form 3: datatype []array_name;
Examples:
int number[];
float average[];
int[] number;
float []marks;

Creating one-dimensional array

array_name=new type[size];
int a[];//declaration
a=new int[5];//creation 5*4=20 bytes continuous
Examples:-
number=new int[5];
average=new float[10];

The declaration and creation can be done together as:-


int number =new int[5];
float average=new float[10];

Overall Syntax of 1-D array is: -

Data type arrayname[]=new data type[size];


int a[]=new int[5];//declaration & creation
4
Programming in JAVA by Roly Sinha

Initializing 1-D array

Initialization is the process to assign variable a value at the time


of variable declaration.

Syntax: data-type array-name[]={V1, V2, V3……Vsize};

eg. int arr[]={10,20,30,40,50};

Storage in Memory: Each element is assigned a block


sequentially, in the order in which it is initialized.
arr
10 20 30 40 50
arr[0] arr[1] arr[2] arr[3] arr[4]

106 110 114 118


102 (base address)
20 byte

Accessing (Input / Output): Once an array is declared, we


must know how to refer an element of an array. This is done
with subscript, the number in the brackets following the array
name. This number specifies the element’s position in the array.
All the array elements are numbered, starting with 0.
Remember the subscript 2 is not the second element of an
array, but the third.
i. Input: Here is the section of code showing how to
populate elements into an array.

for(i=0;i<5;i++)
arr[i]= sc.nextInt();

5
Programming in JAVA by Roly Sinha

The for loop causes the process of asking for and


receiving the value from the user to be repeated five
times. The first execution cycle of the loop has i as 0,
so the input statement places the value typed through
keyboard at 0th position of arr i.e. arr[0]. And as the
loop continues, it will place the rest of the values to
the consecutives position as the value of i increments
by 1 each time
ii. Output: Here is the section of code showing how
to print the value of an array onto the output
screen.
for(i=0;i<5li++)
System.out.println(arr[i]);
It behaves the same way as that of in input case.

2. Two dimensional Array: Its concept can be understand by


the concept of matrix.

a. Denoted by: [][] (double square bracket)


b. Declaration Syntax:
Step1: data-type array-name[][];
Step2: array-name[][] = new data-type[row-size][col-
size];
Here the row-size or col-size will be any non-
negative integer.
eg: int arr[][];
arr[][] = new int [2][3];
You can combine both the steps and create 2D array
as follows: -

data-type array-name[][]=new data-type[row][col];

eg: int arr[][]=new int[2][3];

You might also like