Arrays Functions
Arrays Functions
DATE: 18.09.2024
Contents
• Arrays
• Functions
• Introduction
• Quiz
Introduction
• An array is a container that holds a fixed number of values of a same data type.
• Need of Array: Difficult to manage large number of variable in normal way. The idea of array is to
• Length of the array is fixed and index starts with 0. Can’t access the elements beyond the array
limit.
Types
• Multidimensional Array
• Jagged Array
• Single dimensional array is a collection of items under common name in linear array fashion.
• Declaration
• Syntax Example
dataType[] arrayName; (or) int []salary; //declaration
dataType []arrayName; (or) salary=new int[10]; //Instantiation
dataType arrayName[];
Instantiation of an Array in Java Declaration and Instantiation
arrayReferenceVariable=new dataType[size] int salary[]=new int[10];
Note:
• Array declaration only create reference of array variable.
Create Array
(OR)
• The [] operator is the indication to the compiler we are naming an array object and not an ordinary
variable.
marks[0]=89;
marks[1]=90;
………..
• Array Length: To find the array length java provides inbuilt length variable
int len=marks.length; //assigns the size of marks array in to the variable len
• Array elements are accessed using index value. The index starts with 0.
• Since more number of elements are there in an array, loops are more convenient way to process.
• In general, array elements are accessed and processed using for loop and for….each loop.
/** Output:
* The ArrayDemo1 class implements an application that
Element at index 0: 89
* Illustrate the access array elements
*/ Element at index 1: 90
public class ArrayDemo1 { Element at index 2: 0
public static void main(String[] args) {
int[] marks = new int[3]; ; // create array
marks[0] = 89; //assign values
marks[1] = 90;
System.out.println("Element at index 0: " + marks[0]); //access elements
System.out.println("Element at index 1: " + marks[1]);
System.out.println("Element at index 2: " + marks[2]);
}
}
/**
* The CustomerDetail class implements an application that
* Illustrate the access array elements
*/
public class CustomerDetail {
public static void main(String[] args) {
String[] custName = new String[5]; ; // create array
custName[0] = "Aaron"; //assign values
custName[1] = "Kavin";
custName[2] = "Jesicca";
custName[3] = "Rishabh";
custName[4] = "Vinitha";
/** Output:
* The ArrayDemoForEach class implements an application that
Using for each Loop:
* Illustrate the access array elements using for-each statements
*/ 56
Class ArrayDemoForEach { 84
public static void main(String[] args) {
52
int[] marks = {56, 84, 52, 90, 100};
System.out.println("Using for each Loop:"); 90
for(int i:marks) { 100
System.out.println(i);
}
}
}
/**
* The CustomerDetailForEach class implements an application that
* Illustrate the access array elements using for-each statements
*/
public class CustomerDetailForEach {
public static void main(String[] args) {
String[] custName = new String[5]; ; // create array
custName[0] = "Aaron"; //assign values
custName[1] = "Kavin";
custName[2] = "Jesicca";
custName[3] = "Rishabh";
custName[4] = "Vinitha";
• In general, more than one dimension refer to the multi dimensional array. Its is array of arrays. The
retrieve and processing like single dimensional array.
Syntax:
Syntax:
DataType[][][] ArrayName = new int[TableSize][RowSize][ColumnSize];
int bookCount[][][]= new int[3][3][3];
19 Arrays & Functions | © SmartCliff | Internal | Version 1.0
Arrays
• Data is stored in the form of table or matrix form. It is used to represents the data in different
• Specifically used to find minimum spanning tree and connectivity checking between nodes.
/** Output:
* The ArrayDemo3 class implements an application that illustrate the access of multidimensional 12
array elements */ 34
Class ArrayDemo3{ 56
public static void main(String[] args){
int [][] x = new int[][] {{1,2},{3,4},{5,6}}; // initialize values
for(int i=0; i < x.length; i++) {
// print array elements
for (int j=0; j < x[i].length; j++) {
System.out.print(x[i][j]);
}
System.out.println();
}
}
}
21 Arrays & Functions | © SmartCliff | Internal | Version 1.0
Arrays
/**
* The MovieSeat class implements an application that illustrate the access of multidimensional array elements */
public class MovieSeat {
public static void main(String[] args){
String [][] seatType = new String[][] {{"B","B","A","A","A"},{"A","A","A","B","B"},{"A","B","B","B","B"},{"B","A","A","B","A"}};
int vipcount = 0, premiumcount = 0, regularcount = 0, viptotal = 5, premiumtotal = 10, regulartotal = 5;
System.out.println("--MOVIE SEAT ARRANGEMENT--");
for(int i=0; i < seatType.length; i++) {
if (i==0)
System.out.println("--------VIP SEATS--------");
else if(i==1)
System.out.println("------PREMIUM SEATS------");
else if(i==3)
System.out.println("------REGULAR SEATS------");
• Java supports jagged array. It is array of arrays with different number of columns.
• Jagged Array: In the form of matrix which represents fixed number of rows and different size
columns.
Syntax:
DataType[][] Array_Name = new int[Row_Size][];
int bookNo[][] = new int[3][]; //variable size column
//adding column
bookNo[0] = new int[3]; //0th row contains 3 columns
bookNo[1] = new int[5]; //1st row contains 5 columns
bookNo[2] = new int[1]; //2nd row contains 1 column
• In a jagged array, which is an array of arrays, each inner array can be of a different size. It helps the
• For example:
/**
* The JaggedArray class implements an application that
* Illustrate the jagged array
*/
public class JaggedArray {
public static void main(String[] args) {
int bookNo[][] = new int[3][];
bookNo[0] = new int[] {1,2,3};
bookNo[1] = new int[] {4,5};
bookNo[2] = new int[] {6,7,8,9,10};
System.out.println("Two Dimensional Jagged Array");
for(int j=0;j<bookNo[i].length;j++) {
Two Dimensional Jagged Array
1
System.out.println(bookNo[i][j]+" "); 2
} 3
System.out.println();
4
} 5
}
}
6
7
8
9
10
/**
* The MovieSeat class implements an application that illustrate the access of multidimensional array elements */
public class MovieSeatJaggedArray {
public static void main(String[] args){
String [][] seatType = new String[][] {{"B","A","A"},{"A","A","A","B","B"},{"A","B","B","B","B"},{"B","A","A","A"}};
int vipcount = 0, premiumcount = 0, regularcount = 0, viptotal = 5, premiumtotal = 10, regulartotal = 5;
System.out.println("--MOVIE SEAT ARRANGEMENT--");
for(int i=0; i < seatType.length; i++) {
if (i==0)
System.out.println("--------VIP SEATS--------");
else if(i==1)
System.out.println("------PREMIUM SEATS------");
else if(i==3)
System.out.println("------REGULAR SEATS------");
Quiz
Quiz
C) Both
Quiz
a) Yes b) No
a) Yes
• Introduction
• Function elements
• Quiz
Introduction
• A function/Method is a block of code which perform specific task and the task is executed when the
–It allows code reusability (define once and use multiple times)
Types
– Built-in function: Java has several functions that are readily available for use. In Java, every built-
in function should be part of some class.
– User defined function: Function created by user based on the need of application.
– Builder of the method - responsible for creating the declaration and the definition of the method (i.e.
how it works)
– Caller - somebody (i.e. some portion of code) that uses the method to perform a task
Function Elements
1. Function Definition
– It define the operation of a function. It consists of the function signature followed by the
function body. The function prototype includes function name, parameter list and return type.
2. Function Call
– It means call or invoke the specific function. When a function is invoked, the program control
jumps to the called function to execute the statements that are in the part of that function. Once
the called function is executed the program control passes back to the calling function.
Function Elements
• Function Definition
Syntax:
modifier returnType methodName(parameterlists) // Function signature
{
//Function Body
}
– Modifiers / Specifiers: It defines the visibility of the method i.e. from where it can be accessible in
the application.
Function Elements
• protected: accessible within the class in which it is defined and, in its subclass,(es).
• default (declared/defined without using any modifier) : accessible within same class and
Function Elements
• Function Definition
– The return type : The data type of the value returned by the method or void if does not return a
value.
– Method Name : Name of the function should specify using valid identifier
– Java Naming Convention: It is a single word that should be a verb in lowercase or multi-word,
that begins with a verb in lowercase followed by adjective or noun. After the first word, first letter of
each word should be capitalized. Example: computeAddition, setName
– Parameter list : Comma separated list of the input parameters are defined, preceded with their
data type, within the enclosed parenthesis. If there are no parameters, you must use empty
parentheses ().
– Method body : It is enclosed between braces. The code specifies the task to be done.
42 Arrays & Functions | © SmartCliff | Internal | Version 1.0
Functions
Function Elements
• Function Call
– Static method is invoked by the class name or using object. (Refer Example)
– Example: className.methodName(argumentList)
– Non static method is invoked by the instance/object of the class (We Will discuss later)
– Example: objectName.methodName(argumentList)
Return Values
• Function return types: Function return values of any valid types. It must return data that matches
their return type.
• Example:
public int addTwoInt(int a, int b){
return a+b; // return integer
}
• Arguments and Parameters: The terms parameter and argument can be used for the same thing
– Arguments –An argument is a value that is passed during a function call or calling function.
– Parameters – Parameters used in the function definition or called function. A parameter is a variable
Note:
• During program execution, the values in the actual arguments is assigned to the formal parameter.
Function Elements
//Calling Function
Function Elements
/** Output:
* The MethodDeclareDemo class implements an application that Sum of two integer
* Illustrate the user defined methods(static) */
values :11
class MethodDeclareDemo{
//User Defined Method
public int static addTwoInt(int a, int b){ //a, b is parameters
return a+b;
}
public static void main (String[] args){
int sum;
sum=addTwoInt(5,6); //5, 6 is arguments
System.out.println(“Sum of two integer values :"+ sum);
}
}
47 Arrays & Functions | © SmartCliff | Internal | Version 1.0
Functions
Function Elements
/**
* The FunctionDeclare class implements an application that display the Movie details
* using the user defined methods(static) */
public class FunctionDeclare {
static void getMovieDetail(String moviename,String moviedescription,int movieduration,String movielanguage,
String moviereleasedate,String moviecountry,String moviegenre) {
System.out.println("Movie Title : "+moviename);
System.out.println("Movie Description : "+moviedescription);
System.out.println("Movie Duration : "+movieduration);
System.out.println("Movie Language : "+movielanguage);
System.out.println("Movie Release Date : "+moviereleasedate);
System.out.println("Movie Country : "+moviecountry);
System.out.println("Movie Genre : "+moviegenre);
}
48 Arrays & Functions | © SmartCliff | Internal | Version 1.0
Functions
Function Elements
Function Elements
Output:
-------Movie Detail-------
Movie Title : AAA
Movie Description : Dramaof1945
Movie Duration : 3
Movie Language : English
Movie Release Date : 25/03/2022
Movie Country : XYZ
Movie Genre : THRILLER
---------------------------
Quiz
a)returnType b) methodName
Quiz
Quiz
c) Efficiency