0% found this document useful (0 votes)
36 views12 pages

ARRAYS IN JAVA File

Uploaded by

ved
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)
36 views12 pages

ARRAYS IN JAVA File

Uploaded by

ved
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/ 12

ARRAYS IN JAVA

An array is a collective name given to a group of similar


quantities.These similar quantities could be percentage
marks of 100 students, salaries of 300 employees or ages of 25
students. Thus an array is a collection of similar elements.
Properties of arrays

✓ The elements are denoted as [0],[1],[2]……..[9].


✓ The array index number always starts with 0.
✓ An array with n elements counts from [0] to [n-1].
✓ Each element of the array holds the same type of data.
Types of arrays -
✓Single Dimensional array
✓Double Dimensional array
Accessing the values of an array -
Single Dimensional array -
The format to read and modify the value of an element of an array
is
NameOfArray [index]
Suppose, we have an array named rollNumber with 5 elements and
each of those is of int type.
So, to store the value 34 as the second element of the array, we will
write the following statement-
rollNumber[1] = 34;
To store the value of the second element of the array in a variable a,
we could write –
a = rollNumber[1];
Double Dimensional Array-
Example –
a = marks[2][4];
ch = name[3][5];
Declaration of arrays -
1. Single dimensional array –
data type array name = new Data type [size of array];

int
float
char
double
String
For Example-
int house = new int[25];
char letter = new char[10];
float price = new float[15];
2. Double Dimensional arrays –
data type array name = new data type [rows][columns];

int
float
char
double
String
Double Dimensional Array-
Example –
int marks = new int [2][4];
char name = new char [3][5];
String house= new house [4][3];
Initialization of array –
Single dimensional array –
data type arrayName [ ] = {data values};
Example –
int num [ ] = {5,10,15,20,25,30,35,40};
char vowels [ ] = {‘a’,‘e’,‘i’,‘o’,‘u’};
Double dimensional array –
data type arrayName [ ][ ] = {data values};
int roll[ ][ ] = {{2,3,4},{4,6,8},{9,7,6}};
char board[ ][ ] = {{‘a’,‘b’,‘c’},{‘g’,‘h’,‘i’}};

You might also like