CO_IF_22412_CO2
Manisha Pokharkar, Lecturer, Vidyalankar Polytechnic
Date: 10 February 2021
<<MSBTE LEAD>>
Unit 02 :
Derived Syntactical
Constructs in Java
Written by
Manisha Ashwin Pokharkar
Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Unit Outcome 4 : Write the
programs by implementing
arrays to solve the given
problem.
Manisha Ashwin Pokharkar
Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
Learning Outcome 4-a : Student
should understand the array and
its types
Manisha Ashwin Pokharkar
Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar Polytechnic,
Mumbai
What we will learn today
1. Definition of Array Key takeaways
2 How to create an Array Concept of Array in Java
3 How to initialize the array
4 One Dimensional Array
5 Multidimensional Array
Manisha Ashwin Pokharkar
Lecturer, Department of computer Engineering[NBA Accredited], Vidyalankar
Polytechnic, Mumbai
Page 5 Maharashtra State Board of Technical Education
Concept Map
Page 6 Maharashtra State Board of Technical Education
Learning Objective/ Key learning
► Understand concept of array in Java
Page 7 Maharashtra State Board of Technical Education
Concept Explanation:
► Arrays in Java are homogeneous data structures implemented in Java as objects.
► Arrays store one or more values of a specific data type and provide indexed access to store the same. A
specific element in an array is accessed by its index. Arrays offer a convenient means of grouping related
information.
Obtaining an array is a two-step process.
1. First, you must declare a variable of the desired array type
2. Second, you must allocate the memory that will hold the array, using new, and assign it to the array
variable
Page 8 Maharashtra State Board of Technical Education
Page 9 Maharashtra State Board of Technical Education
One Dimensional Array
► A list of items can be given one variable name using only one subscript and such a variable is called
a single-subscripted variable or a one –dimensional array.
Creating an array
► Array must be declared and created in the computer memory before they are used.
► Creation of array involves three steps:
► declare the array
► create memory locations
► put values into the memory locations
Declaration of an Array
Array can be declared in two forms
Syntax:
1) Type arrayname [ ];
2) data type [ ]arrayname;
Example: int number [ ];
int [ ] numbers;
Page 10 Maharashtra State Board of Technical Education
Creation of array
► After declaring the variable for the array, the array needs to be created in the memory.
► This can be done by using the new operator in the following way:
Arrayname = new type[size];
numbers = new int [10];
► This statement assigns ten contiguous memory locations of the type int to the variable numbers. The
array can store ten elements.
int number [] = new int[10];
Page 11 Maharashtra State Board of Technical Education
Page 12 Maharashtra State Board of Technical Education
Initialization of array
The final step is to put values into the array created. This process is known as
initialization .
Syntax:
Arrayname[subscript] = value;
Example:
number[0] = 50;
number[1] = 60;
we can also initialize array automatically in the same way as ordinary variable
when they are declared.
type arrayname[] = { list of values};
int number[] = { 35,40,50,45,95 };
Page 13 Maharashtra State Board of Technical Education
Array length
In java all array store the allocated size in a variable named length. We can access the length of the
array using a.length
int asize = a.length;
Page 14 Maharashtra State Board of Technical Education
Program
class arraytest
{
public static void main(String args[])
{
int [] a1;
a1=new int[3];
a1[0]=11;
a1[1]=22;
a1[2]=33;
System.out.println("first element= "+a1[0]);
System.out.println("first element= "+a1[1]);
System.out.println("first element= "+a1[2]);
}
}
Page 15 Maharashtra State Board of Technical Education
Two Dimensional Arrays
A two dimensional array can be thought of as a table of rows and columns
Syntax:
data type [] [] variablename;
Example:
int [] [] numbers;
To create the array in the memory, following statement can be use
numbers = new int [3][3];
This will create two dimensional array of 9 elements – three rows and three columns.
Page 16 Maharashtra State Board of Technical Education
Page 17 Maharashtra State Board of Technical Education
Quick Revision
1) Which method is used to calculate the element present in the array?
a) length()
b) capacity()
c) size()
d) All the above
2)Types of Array _________
a) One Dimensional
b) Two Dimensional
c)Both a and b
d)None of the above
Page 18 Maharashtra State Board of Technical Education