UNIT 2.
ARRAYS AND OOP CONSTRUCTS
Arrays, Command Line Arguments, Strings-String Class Methods
Classes & Objects: Creating Classes, declaring objects, Methods, parameter passing, static
fields and methods, Constructors, and ‘this’ keyword, overloading methods and access
Inheritance: Inheritance hierarchies, super and subclasses, member access rules, ‘super’
keyword, preventing inheritance: final classes and methods, the object class and its
methods; Polymorphism: Dynamic binding, method overriding, abstract classes and
methods;
ARRAY
An array is a collection of homogeneous or same type of data items under a common
name.
The data items of array are called as elements or subscripted variables or indexed
variables. Each element is accessed by using index or subscript.
The index value in every array starts with 0 (zero) and ends with [Link] index can be
written in the pair of square brackets following by array name.
Example: int salary [10]; represents the alary of 10th employee.
Types of Arrays:
1. One-Dimensional Array :
A one-dimensional array is also known as linear array that stores multiple values of the
same data type under one name.
List of items are accessed by using only one subscript. Such variable is called a single-
subscripted variable or one-dimensional array.
A one dimensional array represents a row or column of elements.
CREATING AN ARRAY:
Like any other variable array variable must be declared before they can be used in the
program.
Creation of array contains below steps:
1. Declaring an array variable: In java array can be declared as follows
Syntax: datatype arrayname[ ]; (Or) Datatype [ ] arrayname;
Example: int a[5]; or Int [ ] a;
2. Allocating memory for an array: After declaring an array we need to create it in the
memory. Java allows us to create arrays using “new” operator.
Syntax: arrayname=new datatype[size];
Example: a=new int[];
OOP through JAVA II [Link] Computer Science Semester - III
The array declaration and memory allocation can be done in single statement as follows:
Syntax: datatype arrayname[]=new datatype[size];
Example:int a[ ] = new int[5];
3. storing elements into an array (initialization): The process of storing elements is known
as initialization. This can be done using
Syntax: arrayname[index]=value;
Or
datatype arrayname[]={list of values};
Example:int a[2]=50; (or) int a[]={10,20,30,40,50};
Examples for 1-D array:
float salary [ ] = new float [ 40 ];
char ch [ ] = { ‘j’, ‘a’, ‘v’, ‘a’};
char [ ] ch = new char [ 10 ];
String name [ ] = { “java”, “is”, “simple” };
String name [ ] = new String [10];
Example:
public class SingleDimensionalArray {
public static void main(String[] args) {
// Declare and initialize a single-dimensional array
int[] marks = new int[5];
// Assigning values
marks[0] = 85;
marks[1] = 90;
marks[2] = 78;
marks[3] = 92;
marks[4] = 88;
// Accessing and printing elements
for (int i = 0; i < [Link]; i++) {
[Link]("Student " + (i + 1) + ": " + marks[i]);
}
}
}
By L. Pulla Reddy, Lecturer in Computer Science pg. 2
OOP through JAVA II [Link] Computer Science Semester - III
Program: // Java program to read and print single dimensional array
import [Link].*;
class Array1d {
public static void main(String args[]) {
int n;
Scanner sc=new Scanner([Link]);
[Link]("Enter the array size:");
n=[Link]();
int a[]=new int[n];
[Link]("Enter values into the array:");
for(int i=0;i<=n-1;i++)
{
a[i]=[Link]();
}
[Link]("The array elements are:");
for(int i=0;i<=n-1;i++)
{
[Link](a[i]);
}
}
}
2. Multi-Dimensional Arrays
Multi-dimensional arrays in Java are arrays of arrays, allowing data to be stored in table-
like (2D) or cube-like (3D) structures. These are used in applications involving matrices,
grids, charts, game boards, or even higher-dimensional scientific calculations.
Two-Dimensional Arrays:-
A two dimensional array are used to represents data values in the form of table i.e.,
rows and columns. A two dimensional array is simply array of arrays. Each element of two
dimensional array can be accessed by using two indices i.e., row index and column index.
Like one-dimensional array, two dimensional arrays must be declared before used in the
program.
By L. Pulla Reddy, Lecturer in Computer Science pg. 3
OOP through JAVA II [Link] Computer Science Semester - III
Syntax: datatype arrayname[][];
Or
Datatype [][] arrayname;
Example:int a[][];
a=new int[2][2];
or
int [][] = new int[2][2];
initialization of two-dimensional array:
While initializing 2D array we don’t provide the size of the array. It will determine from the
values which you given.
Syntax: arrayname[row][column] = value;
Example:
int[] marks = new int[2][2]; //declaration of 2D array
// initialize the array elements
marks [0][0] = 15;
marks [0][1] = 10;
marks [1][0] = 25;
marks [1][1] = 20;
Example:
public class TwoDimensionalArray {
public static void main(String[] args) {
int[][] matrix = new int[2][3];
// Assign values
matrix[0][0] = 10;
matrix[0][1] = 20;
matrix[0][2] = 30;
matrix[1][0] = 40;
matrix[1][1] = 50;
matrix[1][2] = 60;
// Print 2D array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
By L. Pulla Reddy, Lecturer in Computer Science pg. 4
OOP through JAVA II [Link] Computer Science Semester - III
Example: //[Link]
import [Link].*;
class Array2Demo
{
public static void main(String[] args)
{
int a[][];
int m,n;
DataInputStream in=new DataInputStream([Link]);
[Link]("Enter the number of rows for matrix:");
m=[Link]([Link]());
[Link]("Enter the number of Columns for matrix:");
n=[Link]([Link]());
a=new int[m][n];
[Link]("Enter the elements into matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=[Link]([Link]());
}
}
[Link]("The matrix elements are:");
for(int i=0;i<=n-1;i++)
{
for(int j=0;j<n;j++)
{
[Link](" " + a[i][j]);
}
[Link]("\n");
}
}
}
By L. Pulla Reddy, Lecturer in Computer Science pg. 5
OOP through JAVA II [Link] Computer Science Semester - III
3D Array (Three-Dimensional Array)
A 3D array stores data in layers or blocks. Think of it as a collection of multiple 2D arrays
stacked on top of each other. It's useful in simulations, games, and advanced data
modeling.
Syntax: dataType[][][] arrayName = new dataType[depth][rows][columns];
Example:
public class ThreeDimensionalArray {
public static void main(String[] args) {
int[][][] cube = new int[2][2][3];
// Assign values
int value = 1;
for (int i = 0; i < 2; i++) { // depth
for (int j = 0; j < 2; j++) { // rows
for (int k = 0; k < 3; k++) { // columns
cube[i][j][k] = value++;
}
}
}
// Print 3D array
for (int i = 0; i < 2; i++) {
[Link]("Layer " + (i + 1));
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
[Link](cube[i][j][k] + " ");
}
[Link]();
}
[Link]();
}
}
}
By L. Pulla Reddy, Lecturer in Computer Science pg. 6
OOP through JAVA II [Link] Computer Science Semester - III
Common Operations on Arrays in Java
Operation Description Sample Syntax / Example
Declaration Creating a reference int[] arr;
to an array
Initialization Allocating memory int[] arr = new int[5];
and assigning values
Assignment Setting a value at a arr[0] = 10;
specific index
Accessing Elements Reading a value [Link](arr[2]);
from a specific index
Finding Length Getting total [Link]
number of elements
Traversing Iterating through for(int i=0; i<[Link]; i++)
(for loop) elements using
index
Enhanced for loop Simpler loop to for(int num : arr)
iterate elements
Copying Arrays Copy one array into [Link](src, 0, dest,
another 0, length);
Sorting Sorting elements in [Link](arr);
ascending order
Searching Finding an element [Link](arr, key);
(binary search
requires sorted
array)
Filling Array Fill entire array with [Link](arr, 1);
a value
Converting to Convert array to [Link](arr);
String printable string
Multidimensional Accessing elements matrix[i][j];, cube[x][y][z];
Access in 2D/3D arrays
By L. Pulla Reddy, Lecturer in Computer Science pg. 7
OOP through JAVA II [Link] Computer Science Semester - III
Q. Length of Array
Every array in Java has a built-in property called “.length” that returns the total number of
elements present in the array. This is not a method, but a public final variable, so it doesn’t
require parentheses.
Syntax: [Link]
Example:
By L. Pulla Reddy, Lecturer in Computer Science pg. 8
OOP through JAVA II [Link] Computer Science Semester - III
class Ex
{
public static void main(String args[])
{
int a[]={55,40,80,65,71};
int n=[Link];
[Link]("Length of given Array is:+n);
}
}
Example:
public class ArrayLengthExample {
public static void main(String[] args) {
// Declare and initialize an array
int[] numbers = {10, 20, 30, 40, 50};
// Find and print the length of the array
[Link]("Length of the array: " + [Link]);
// Use length to safely loop through elements
[Link]("Array elements:");
for (int i = 0; i < [Link]; i++) {
[Link]("Index " + i + ": " + numbers[i]);
}
}
}
By L. Pulla Reddy, Lecturer in Computer Science
pg. 9