Chapter 2 Non Primitive Data Types
Chapter 2 Non Primitive Data Types
Chapter Objectives
In this chapter, you will learn more about what is a Data Structure and Algorithms is and
how it actually functions. At the end of this chapter, students will be able to:
Create a way to apply the properties of Non primitive data type
Design and implement array and its application
Apply the two addressing methods - computed addressing and link addressing
A Non-primitive data type is further divided into Linear and Non-Linear data structure
Array: An array is a fixed-size sequenced collection of elements of the same data type.
List: An ordered set containing variable number of elements is called as Lists.
File: A file is a collection of logically related information. It can be viewed as a large list of
records consisting of various fields.
1. Array
It is a finite sequence of data items occupying a continuous set of memory locations, all
of which are of the same type.
Set of homogeneous elements.
An array is a data structure
Used to process multiple elements with the same data type when a number of such
elements are known.
An array is a composite data structure; that means it had to be constructed from basic
data types such as array integers.
Direct or random access means that each element can be accessed simply by specifying its
location in the array. Sequential access means that an element can only be accessed by first
processing all those that precede it. Index or subscript specifies the position of an element in the
array. One-dimensional Array requires only one index to access an element. Base address refers
to the address of the first memory cell used to store the elements. Multi-dimensional Array
requires 2 or more indices to access a single element of the array.
0 1 2 3
2. Two dimensional Array
int B[5][4];
0 1 2 3
0
1
2
3
4
0 1 2 3
0 0
3. 3 dimensional Array 1 1
int C[3][5][4]; 2 2
3
4
4. 4 dimensional Array
int D[2][3][5][4];
0 1 2 3 0 1 2 3
0 0 0
0
1 1 1
1
2 2 2
2
3 3
4 4
0
1
Declaring Arrays
When declaring an array, you list the data type, followed by a set of square brackets [],
followed by the identifier name. For example,
int []ages;
or you can place the brackets after the identifier. For example,
int ages[];
After declaring, we must create the array and specify its length with a constructor statement.
This process in Java is called instantiation (the Java word for creates). In order to instantiate an
object, we need to use a constructor for this. Take note, that the size of an array cannot be
changed once you've initialized it. For example,
//declaration
int ages[];
//instantiate object
ages = new int[100];
or, can also be written as,
//declare and instantiate object
int ages[] = new int[100];
In the example, the declaration tells the Java Compiler that the identifier ages will be used as
the name of an array containing integers, and to create or instantiate a new array containing
100 elements.
Instead of using the new keyword to instantiate an array, you can also automatically declare,
construct and assign values at once.
Examples are,
//creates an array of Boolean variables with identifier
//results. This array contains 4 elements that are
//initialized to values {true, false, true, false}
boolean results[] ={ true, false, true, false };
//creates an array of 4 double variables initialized
//to the values {100, 90, 80, 75};
double []grades = {100, 90, 80, 75};
Take note that once an array is declared and constructed, the stored value of each member of
the array will be initialized to zero for number data. However, reference data types such as Strings
are not initialized to blanks or an empty string “”. Therefore, you must populate the String arrays
explicitly. The following is a sample code on how to print all the elements in the array. This uses
a for loop, so our code is shorter.
Coding Guidelines:
1. It is usually better to initialize or instantiate the array right away after you declare it.
For example, the declaration,
int []arr = new int[100];
is preferred over,
int []arr;
arr = new int[100];
2. The elements of an n-element array have indexes from 0 to n-1. Note that there is no
array element arr[n]! This will result in an array-index-out-of-bounds exception.
3. You cannot resize an array.
Array length
In order to get the number of elements in an array, you can use the length field of an array. The
length field of an array returns the size of the array. It can be used by writing,
arrayName.length
For example, given the previous example, we can re-write it as,
Coding Guidelines:
1.When creating for loops to process the elements of an array, use the array object's
length field in the condition statement of the for loop. This will allow the loop to adjust
automatically for different-sized arrays.
2.Declare the sizes of arrays in a Java program using named constants to make them easy
to change. For example,
Multidimensional Arrays
Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays are
declared by appending the appropriate number of bracket pairs after the array name. For
example,
To access an element in a multidimensional array is just the same as accessing the elements in a
one dimensional array. For example, to access the first element in the first row of the array dogs,
we write,
System.out.print( dogs[0][0] );
Addressing Methods
In creating a data structure, it is important to determine how to access the data items. It is
determined by the addressing method used. There are two types of addressing methods in
general:
Computed addressing method is used to access the elements of a structure in preallocated space.
It is essentially static, an array for example:
A data item can be accessed directly by knowing the index on where it is stored.
Example
1. short int A[4];
find A[3]
Solution:
esize = 2 =Short int
e= 3
2. Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2
bytes in the memory. Find the address of B[1700].
Solution:
Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
Row wise
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
Colum wise
where,
B is the array name
i is index row =I = ( I – Lr )
j is index column =J = ( J – Lc )
c is the total number of columns = N
d is the total number of columns = M
esize is the size (in bytes) occupied by the element of the array = W
NOTE: Usually number of rows and columns of a matrix are given (like A [20][30] or A[40][60] )
but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc]. In this case number of rows and columns are
calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1
Number of columns (N) will be calculated as = (Uc – Lc) + 1
http://www.guideforschool.com/625348-memory-address-calculation-in-an-array/
And rest of the process will remain same as per requirement (Row Major Wise or Column Major
Wise).
Example.
Solution:
As you see here the number of rows and columns are not given in the question. So they
are calculated as:
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
2. double x[36][42];
Find x[23][38] using column and row wise
Solution:
c= total column size = 42
d= total row size = 36
esize =double = 8
i= 23
j= 38
row wise
= base(B) + 8032
column wise
= base(B) + 11128
Applications of Array
a. Sparse Matrix
b. Symbol Manipulation using Array
We can use array for different kind of operations in polynomial equation such as addition,
subtraction, division, differentiation etc…
We are interested in finding suitable representation for polynomial so that different operations
like addition, subtraction etc… can be performed in efficient manner
Example:-
The non-zero entries of a sparse matrix may be mapped into a linear list in row-major
order.
1. For example the non-zero entries of 4X8 matrix of below fig.(a) in row major order are
2, 1, 6, 7, 3, 9, 8, 4, 5
To construct matrix structure we need to record
A corresponding amount of time is saved creating the linear list representation over initialization
of two dimension array.
2. Here from 6X7=42 elements, only 10 are non-zero. A[1,3]=6, A[1,5]=9, A[2,1]=2,
A[2,4]=7, A[2,5]=8, A[2,7]=4, A[3,1]=10, A[4,3]=12, A[6,4]=3, A[6,7]=5.
One basic method for storing such a sparse matrix is to store non-zero elements in one
dimensional array and to identify each array elements with row and column indices fig
(c).