0% found this document useful (0 votes)
38 views22 pages

Lec - 04 - Arrays

This document covers topics about arrays in object oriented programming, including single and multi-dimensional arrays. It defines an array as a group of like-typed variables referred to by a common name, with individual variables accessed by their index. There are two types of arrays: single dimensional arrays and multi-dimensional arrays. The document discusses array declaration, creation, initialization, indexing, passing arrays to methods, and pros and cons of using arrays.

Uploaded by

muhammad khubab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views22 pages

Lec - 04 - Arrays

This document covers topics about arrays in object oriented programming, including single and multi-dimensional arrays. It defines an array as a group of like-typed variables referred to by a common name, with individual variables accessed by their index. There are two types of arrays: single dimensional arrays and multi-dimensional arrays. The document discusses array declaration, creation, initialization, indexing, passing arrays to methods, and pros and cons of using arrays.

Uploaded by

muhammad khubab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Oriented

Programming
Topics To Be Covered Today
 Array
◦ Single & Multi-dimensional
Arrays
 An array is a group of liked-typed variables
referred to by a common name, with individual
variables accessed by their index.
 Normally, array is a collection of similar type of

elements that have contiguous memory location.

 First element of the array is stored at 0 index.


Types of Arrays
 There are two types of array.
◦ Single Dimensional Array
◦ Multidimensional Array

 Arrays are:
1) Declared
2) Created (instantiated)
3) Initialized
4) Used
Array Declaration
 Array declaration involves:
1) declaring an array identifier
2) declaring the number of dimensions
3) declaring the data type of the array elements
 Three styles of array declaration:
type array-variable[ ];
e.g. int abc[ ];
or
type [ ]array-variable; Datatype => int
Identifier => abc
or Dimention=> single
type[ ] array-variable;
Array Creation
 After declaration, no array actually exists.
 In order to create an array, we use the new

operator:
type array-variable[ ];
array-variable = new type[size];
 This creates a new array to hold size elements of

type type, whose reference will be kept in the


variable array-variable.
Array Creation
Array Indexing
 Later we can refer to the elements of this array
through their indexes:
array-variable[index]
 The array index always starts with zero!
 The Java run-time system makes sure that all

array indexes are in the correct range, otherwise


raises a run-time error.
Example: Array Use
Array Initialization
 Arrays can be initialized when they are declared:
int monthDays[ ] =
{31,28,31,30,31,30,31,31,30,31,30,31};

Comments:
1) there is no need to use the new operator
2) the array is created large enough to hold all specified
elements
Example: Array Initialization
Passing Array to method in java
Multi-dimensional Array
 Multidimensional arrays are arrays of arrays:
1) declaration
int array[ ][ ];
2) creation
int array[ ][ ] = new int[2][3];
3) initialization
int array[ ][ ] = { {1, 2, 3}, {4, 5, 6} };
Example: Multi-dimensional Array
class name of java array?
 In java, array is an object.
 For array object, a proxy class is created whose

name can be obtained by getClass().getName()


method on the object.
Example
Copying a java array
 An array can be copied to another by the
arraycopy() method of System class

 Syntax of arraycopy method

arraycopy(Object src, int srcPos,Object dest, int destPos, 
int length)
Example

You can use for loop to print dest array:

for(int i=0;i<copyTo.length;i++)
System.out.println(copyTo[i]);
Pros and Cons of Arrays
Advantages Disadvantages
 Code Optimization  Size Limit 

◦ It makes the code ◦ We can store only fixed


optimized, we can size of elements in the
array. It doesn't grow its
retrieve or sort the
size at runtime.
data easily.
 Random access 
◦ We can get any data
located at any index
position.
Class Participation
Questions?

You might also like