Template: Study Material
Insert the details within the < quotes >
<CO2>: <22412>: <Java Programming>: <Derived Syntactical Constructs in Java>: <LO4>: <Study Material>
<Manish Pokharkar> <> <Vijay Patil>
Key words Learning Objective: Diagram/
array student should understand the array and its types Picture
Key Questions Concept Map
Write program for One
dimensional array.
public class
OneDimesionalArray
{
// Java program to illustrate
store and print marks of 5
students
public static void main (String[]
args)
{
// declaring an Array of
integers.
int[] marks;
// allocating memory for 5
integers.
marks = new int[5]; Concept Explanation:
// initializing the first element
of the array
Arrays in Java are homogeneous data structures implemented in Java as
marks[0] = 78;
objects. Arrays store one or more values of a specific data type and provide
// initializing the second
indexed access to store the same. A specific element in an array is accessed by
element of the array and so
its index. Arrays offer a convenient means of grouping related information.
on..
Obtaining an array is a two-step process.
marks[1] = 85;
marks[2] = 97;
marks[3] = 66; First, you must declare a variable of the desired array type
marks[4] = 70; Second, you must allocate the memory that will hold the array,
// accessing the elements of using new, and assign it to the array variable
the specified array
for (int iterator = 0; iterator < So, let us see how can we declare arrays in different ways.
marks.length; iterator++)
System.out.println("Marks of
General Form of Java Array Initialization
Student " + (iterator+1) +": "+
marks[iterator]);
}
}
Write program for two-
dimensional arrays:
public class
TwoDimensionalArrays Example:- int month_days[];
{
Key
public static void main (String[]
Definitions/
args) Formulas
General Form of Java Array Initialization
{
//declaring and initializing a
two-dimensional array
int[][] myArray = new int[3][3];
myArray[0][0]=1;
myArray[0][1]=2;
myArray[0][2]=3;
myArray[1][0]=4;
myArray[1][1]=5;
myArray[1][2]=6;
myArray[2][0]=7;
myArray[2][1]=8;
myArray[2][2]=9; Example:-
//printing a two-dimensional
array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(myArray[i][j]
+" ");
}
System.out.println();
}
}
}
Arrays can be initialized when they are declared. The array will automatically
be created large enough to hold the number of elements you specify in the
array initializer. There is no need to use new.
General Form of Java Array Initialization
Types of Array:
Types of Array
One Dimentional Multidimentional
1)Single Dimensional / One Dimensional Array:
The simplest form of an array is a single-dimensional array. It has elements
in a single dimension. Such as a variable, method, and class, we can give a
name to the array. And, we refer to array elements by their subscripts or
indices.
Declaring one-dimensional arrays
syntax for array declaration
data-type array_name [ ];
data-type [ ] array_name;
Example:
int daysInMonth [ ];
char [ ] lettersInSentence;
double salaryOfEmployees [ ];
String progLanguages[ ];
Initializing one-dimensional arrays
After declaring an array, we allocate the memory to the array with the help of
a new operator and assign the memory to the array variable. So, let us see how
we can initialize arrays in different ways. The variable size of the array should
be of constant integer type without any sign (+ or -).
syntax for array initialization
array_name = new data-type [size of array];
array_name = new data-type {elements of array using commas};
Example
daysInMonth = new int [100];
lettersInSentence = new char[5];
salaryOfEmployees = new double[ ] {10000, 50000, 30000, 25000 };
progLanguages = { “C”, “Java”, “Ruby”, “Python”, “PHP” };
Multi-Dimensional Arrays
Multi-dimensional arrays are arrays of arrays in which each element of the
array holds the reference of other arrays. We can also call them Jagged Arrays.
The most commonly used multi-dimensional array is a two-dimensional array.
A two-dimensional array is an array in which each element is a one-
dimensional array.
For example, a two-dimensional array myArray [ M ][ N ] is an M by N table
with M rows and N columns containing M N elements.
By multiplying the number of rows with the number of columns we can
determine the number of elements in a two-dimensional array. For example,
the number of elements in an array arr [7][9] is calculated as 7 9 = 63.
Syntax of declaring two-dimensional arrays is:
type array-name [ ][ ]= new type[number of rows] [number of columns];
Example:
int myArray [ ] [ ] = new int [5] [12] ;
or
int[ ] [ ] myArray = new int[5] [12] ;
The array myArray has 5 elements myArray [0], myArray[1], myArray[3], and
myArray[4], each of which itself is an int array with 12 elements. The elements
of myArray can be referred to as myArray[0][0], myArray[0][1], myArray[0][2],
…….., myArray[0][12], myArray [1][0], ……. and so forth.
Application of Concept/ Examples in real life
Key Take away from this LO:
Concept of array in Java