dsa2 arrays(061808)
TRANSCRIPT
-
8/14/2019 DSA2 Arrays(061808)
1/18
Data Structures andData Structures andAlgorithms (Arrays)Algorithms (Arrays)
Jose Rizal University, Mandaluyong City, Philippines
-
8/14/2019 DSA2 Arrays(061808)
2/18
2
References:
Data Structures and Algorithms in JAVA
Goodrich and Tamassia (2004) John Wiley &
Sons, Inc. Data Structures & Algorithm Analysisin C Mark Allan Weiss. (1993) Benjamin
Cummings Publishing Company,
Inc./Jemma, Inc., Philippines
-
8/14/2019 DSA2 Arrays(061808)
3/18
-
8/14/2019 DSA2 Arrays(061808)
4/18
4
QuestionsQuestions
How important are these primitive data
types in developing our programs?
What limitations regarding the use ofprimitive data types can you imagine?
-
8/14/2019 DSA2 Arrays(061808)
5/18
5
Introducing ArraysIntroducing Arrays
Arrays are a very useful data structure provided by Java and otherprogramming languages.
An array is a variable compose of a finite, ordered set of homogeneouselements, i.e. elements of the same data type
Individual elements of this sequence can be uniformly
referenced/updated/ etc. since it consumes a consecutive set ofmemory locations is a set of pairs - index and a value forms
one-dimensional array n-dimensional array
Question:
Based on the definition above, how can arrays address the limitationsof using primitive data types ?
0
1
2
3
4
5
-
8/14/2019 DSA2 Arrays(061808)
6/18
6
Creating ArraysCreating Arrays
Arrays in Java are handled as objects (hence allocatedon heap)
2 data types base type or component type
index type All data types (primitive and user-defined objects) canbe put into arrays
To create an array, use the newkeyword Java has special syntax for array type declaration:
int[] a = new int[5]; int[] indicates a is array of integer variables int[5] indicates a will have 5 elements
Each cells in the array are assigned default values (0 /null / etc.) when array created
-
8/14/2019 DSA2 Arrays(061808)
7/18
7
Array IndexingArray Indexing
Java provides a special syntax for uniformly accessing cells in anarray Assume previous declaration of a:
int[] a = new int[5];
This in effect creates five int variables named: a[0] a[1] a[2] a[3] a[4]
To modify contents of cell #2 to 6: a[2] = 6;
This access mechanism is called array indexing 2 basic operations
extraction and storing
In Java, the same as with C and C++, array cells are indexedbeginning at 0 and going up to n-1 (n is number of cells) Beware of index numbers that exceed the array structure. Also, beware of starting the indexing at 0!
-
8/14/2019 DSA2 Arrays(061808)
8/18
8
ExampleExample
If we use the following codes:
int[] arr1 = new int[5];
arr1[2] = 6;
Which cell in arr1 would contain the value
6?
-
8/14/2019 DSA2 Arrays(061808)
9/18
9
0 1 2 3 4
6
Cell Index numbers
-
8/14/2019 DSA2 Arrays(061808)
10/18
10
Handling arrays usingHandling arrays using
loopsloops
Loops Are Useful for Processing Arrays
Loop counter can be used to iteratethrough index values
Each iteration of loop thus processes adifferent array element
-
8/14/2019 DSA2 Arrays(061808)
11/18
11
Examples of handlingExamples of handling
arraysarrays
int i;
int[] arr1 = new int[5];
arr1[2] = 6;for (i=0; i
-
8/14/2019 DSA2 Arrays(061808)
12/18
12
Exercise QuestionsExercise Questions
In the above coding, how would you changethe size of the array to accommodate 10integer values instead of 5?
What would happen if: instead of i++, i was used in the for loop
statement? if instead of i=0, i=1 was used? If instead of i
-
8/14/2019 DSA2 Arrays(061808)
13/18
13
If an array is declared to be A[n], then:
n = number of elements
If an array is declared to be A[n][m], then:
n*m = number of elements
If given n-dimensional array declaration,
A[b][c][d][e]..[n] = b,c,..n
-
8/14/2019 DSA2 Arrays(061808)
14/18
14
To determine the ith element of a single-dimensionarray: A[i] = + (i) * esize
where:
- base or starting addressi - element
esize - element size in bytes
2-dimensional arrays:A[i][j] = + [(i) * (UB2) + (j)] * esize
where: UB2 = upper bound of the 2nd dimension.
-
8/14/2019 DSA2 Arrays(061808)
15/18
15
3-dimensional arrays:
A[i][j][k] = + [(i) * (UB2) *(UB3) + (j)*(UB3)
+ (k)] * esize
where:UB2 = upper bound of the 2nd dimension;
UB3 = upper bound of the 3rd dimension
esize = element size in bytes
Typical esize: integer - 4 byteschar - 1 byte
float - 4 bytes
-
8/14/2019 DSA2 Arrays(061808)
16/18
16
ExamplesExamples
1. Given A[10][3][3][6], = 2000, esize=4
bytes:
a. find the formula to represent anelement in a 4-dimensional array.
b. find the total number of elements
c. find the address of A[2][2][0][4]
-
8/14/2019 DSA2 Arrays(061808)
17/18
17
ExamplesExamples
2. Given X[8][3][6][2][3], = 3000, esize = 3
bytes
a. find the total number of elementsb. find the address of X[0][2][5][1][2]
-
8/14/2019 DSA2 Arrays(061808)
18/18
18
ExamplesExamples
3. Consider the following declaration:class Myclass {
int A;char [] B = new char[10];
float C;char D;}Myclass class1 = new matrix [121][4][5];matrix A1;
A. Compute the address of element A1[120][3][3] given that thebase address is 2000.
B. Assume that we do not the size of Myclass in number of bytesbut we know that the address of A1[20][2][3] is 2160. Give thesize of Myclass in bytes. Assume the base address is 2000.