Sunteți pe pagina 1din 25

CS334

PROGRAMMING PARADIGMS
Object Oriented Programming using Java
Arrays

MISSION VISION CORE VALUES


CHRIST is a nurturing ground for an individual’s Excellence and Service Faith in God | Moral Uprightness
holistic development to make effective contribution to Love of Fellow Beings
the society in a dynamic environment Social Responsibility | Pursuit of Excellence
CHRIST
Deemed to be University

Arrays
● an array is a collection of similar type of elements that have a contiguous
memory location.
● Data Structure – organizing , storing and retrieving.

Excellence and Service


CHRIST
Deemed to be University

Array ( Cond…)

Advantages
● Code Optimization: It makes the code optimized, we can retrieve or sort the
data efficiently.
● Random access: We can get any data located at an index position.

Disadvantages
● Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection framework is
used in Java which grows automatically.

Excellence and Service


CHRIST
Deemed to be University

Types of Array in java

● Single Dimensional Array - 1 D

arrayName []

● Multidimensional Array - 2D,3D,4D etc.

ArrayName[][]
ArrayName[][][]
ArrayName[][][][]

Excellence and Service


CHRIST
Deemed to be University

Single Dimensional Array

Syntax to Declare an Array


■ dataType[] arr; (or)
■ dataType []arr; (or)
■ dataType arr[];

● Eg : int arr[]; int[] arr; int []arr;

Instantiation of an Array in Java


■ arrayRefVar=new datatype[size];

● Eg : int arr[]=new int[5];

Excellence and Service


CHRIST
Deemed to be University

Example

class Testarray{
public static void main(String args[]){
int a[]=new int[5]; //declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length; i++)//length is the property of array
System.out.println(a[i]);
}}

Excellence and Service


CHRIST
Deemed to be University

For other data types - Declaration

● byte Array[];
● short Array[];
● boolean Array[];
● long Array[];
● float Array[];
● double Array[];
● char Array[];

Excellence and Service


CHRIST
Deemed to be University

For other data types – and Instantiation

● byte Array[] = new byte[5];


● short Array[] = new short[10];
● boolean Array[] = new boolean[8];
● long Array[] = new long[4];
● float Array[] = new float[3];
● double Array[] = new double[10];
● char Array[] = new char[20];

Excellence and Service


CHRIST
Deemed to be University

Declaration, Instantiation and Initialization

● int a[]={33,3,4,5};

● A[0] = 33;
● A[1] = 3;
● A[2] = 4;
● A[3] = 5;

Excellence and Service


CHRIST
Deemed to be University

Single dimension - “array of Objects”


class Student Declaration of array of object :
{ Student s1[];
int x,y; Instantiation:
} Student s[] = new Student[3];
Initialization:
Obejct Creation : S[0]= new Student();
Student s1=new Student(); S[1]= new Student();
Student s2=new Student();
Student s3=new Student(); Accessing variables :
Accessing variables : S[0].x, s[0].y
s1.x, s1.y S[1].x, s[1].y
s2.x, s2.y S[2].x, s[2].y
s2.x, s2.y

Excellence and Service


CHRIST
Deemed to be University

Multidimensional Array in Java – 2D

Declare Multidimensional Array


● dataType[][] arrayRefVar; (or)
● dataType [][]arrayRefVar; (or)
● dataType arrayRefVar[][]; (or)
● dataType []arrayRefVar[];
instantiate Multidimensional Array
● int[][] arr=new int[3][3];
initialize Multidimensional Array
arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;

Excellence and Service


CHRIST
Deemed to be University

Fixed length array

● int[][] arr=new int[3][3];

Excellence and Service


CHRIST
Deemed to be University

Variable length array

Excellence and Service


CHRIST
Deemed to be University

Variable length array

int twoD[][] = new int[4][];

twoD[0] = new int[1];

twoD[1] = new int[2];

twoD[2] = new int[3];

twoD[3] = new int[4];

Excellence and Service


CHRIST
Deemed to be University

Emplyee class – array of Employee class


public class Main{
class Employee { public static void main(String[] args) {
int Employee_ID; Employee E[] = new Employee[3];
String Name; E[0]= new Employee();
String Department; E[1]= new Employee();
int Salary; E[2]= new Employee();

void setValues(int id, String E[0].setValues(1,"ANU","A",10000);


n, String d, int s) E[1].setValues(1,"BANU","B",7000);
{ Employee_ID=id; E[2].setValues(1,"JANU","C",11000);
Name=n;
Department=d; if (E[0].Salary< E[1].Salary &&
Salary=s; E[0].Salary< E[2].Salary)
} System.out.println("Employee 1
} Salary is minimum salary");
}}

Excellence and Service


CHRIST
Deemed to be University

Comments in Java

● Single line comment


● Multi line comment
● Documentation Comment

Excellence and Service


CHRIST
Deemed to be University

Single line comment

● begins with // and ends at the end of the line.

public class CommentsDemo1


{
public static void main(String args[])
{
//I am a single line comment
System.out.println("Hi World");
}
}

Excellence and Service


CHRIST
Deemed to be University

Multi line comment

● begins with /* and ends with */ that spans multiple lines.


public class CommentsDemo2
{
public static void main(String args[])
{

/* This is a
multi line
comment
*/
System.out.println("Hi World");
}
}

Excellence and Service


CHRIST
Deemed to be University

Documentation Comment

● begin with /** and terminate with */ and that spans multiple lines.
/**
This is a documentation comment.

This class is written to show the use of documentation


comment in Java. This program displays the text "Hi World"
*/
public class CommentsDemo3
{
public static void main(String args[])
{
System.out.println("Hi World");
}
}

Excellence and Service


CHRIST
Deemed to be University

Why ?

/**
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
*
* @author Zara Ali
* @version 1.0
* @since 2014-03-31
*/

- By using this, It will generate the documents automatically.

Excellence and Service


CHRIST
Deemed to be University

List of Document Comments


COMMENT DESCRIPTION SYNTAX

@author Adds the author of a class. @author name-text

@param Adds a parameter with the specified @param parameter-


parameter-name followed by the specified name description
description to the "Parameters" section.

@return Adds a "Returns" section with the @return description


description text.


@versio Adds a "Version" subheading with the @version version-
n specified version-text to the generated text
docs when the -version option is used.

Excellence and Service


CHRIST
Deemed to be University

Example

● public class AddNum {


● /**
● * This method is used to add two integers. This is
● * a the simplest form of a class method, just to
● * show the usage of various javadoc Tags.
● * @param numA This is the first paramter to addNum method
● * @param numB This is the second parameter to addNum method
● * @return int This returns sum of numA and numB.
● */
● public int addNum(int numA, int numB) {
● return numA + numB;
● }

Excellence and Service


CHRIST
Deemed to be University

List of Document Comments


COMMENT DESCRIPTION SYNTAX

{@code} Displays text in code font without interpreting {@code text}


the text as HTML markup or nested javadoc
tags.

{@docRo Represents the relative path to the generated {@docRoot}


ot} document's root directory from any generated
page.

@excepti Adds a Throws subheading to the generated @exception class-


on documentation, with the classname and name description
description text.

{@link} Inserts an in-line link with the visible text label {@link
that points to the documentation for the package.class#memb
specified package, class, or member name of er label}
a referenced class.

Excellence and Service


CHRIST
Deemed to be University

List of Document Comments

COMMENT DESCRIPTION SYNTAX

@see Adds a "See Also" heading with a link or text @see reference
entry that points to reference.

@since Adds a "Since" heading with the specified @since release


since-text to the generated documentation.

{@value} When {@value} is used in the doc comment {@value


● of a static field, it displays the value of that package.class#field}
constant.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service

S-ar putea să vă placă și