Docsity

Array Representation & Memory Allocation in Java, C, C++: 1D/2D Arrays, Mapping, Irregular, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Array Representation & Memory Allocation in Java, C, C++: 1D/2D Arrays, Mapping, Irregular and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Arrays

1D Array Representation In Java, C, and C++

Memory a^

b^

c^

d

start

  • 1-dimensional array x = [a, b, c, d] • map into contiguous memory locations^ • location(x[i]) = start + i

2D Arrays

The elements of a 2-dimensional array a

declared as: int [][]a = new int[3][4]; may be shown as a table

a[0][0]

a[0][1]

a[0][2]

a[0][3]

a[1][0]

a[1][1]

a[1][2]

a[1][3]

a[2][0]

a[2][1]

a[2][2]

a[2][3]

Rows Of A 2D Array

a[0][0]

a[0][1]

a[0][2]

a[0][3]

row 0

a[1][0]

a[1][1]

a[1][2]

a[1][3]

row 1

a[2][0]

a[2][1]

a[2][2]

a[2][3]

row 2

2D Array Representation In Java, C, and C++

2-dimensional array x

a, b, c, d e, f, g, h^ i, j, k, l

view 2D array as a 1D array of rows

x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 4 1D arrays

2D Array Representation In Java, C, and C++

a^

b^

c^

d

e^

f^

g^

h

i^

j^

k^

l

x[]

x.length = 3 x[0].length = x[1].length = x[2].length = 4

Array Representation In Java, C, and C++

a^

b^

c^

d

e^

f^

g^

h

i^

j^

k^

l

x[]

-^

This representation is called the array-of-arraysrepresentation.

-^

Requires contiguous memory of size 3, 4, 4, and 4 for the4 1D arrays.

-^

1 memory block of size number of rows and number ofrows blocks of size number of columns

Row-Major Mapping

  • Example 3 x 4 array:

a b c d^ e f g h^ i j k l

-^

Convert into 1D array y by collecting elements by rows.

-^

Within a row elements are collected from left to right.

-^

Rows are collected from top to bottom.

-^

We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l}

row 0

row 1

row 2

…

row i

Space Overhead

row 0

row 1

row 2

…

row i

4 bytes for start of 1D array + 4 bytes for length of 1D array + 4 bytes for c (number of columns) = 12 bytes (number of rows = length /c)

Disadvantage

Need contiguous memory of size rc.

Matrix

Table of values. Has rows and columns, but

numbering begins at 1 rather than 0.

a b c d

row 1

e f g h

row 2

i j k l

row 3

  • Use notation x(i,j) rather than x[i][j]. • May use a 2D array to represent a matrix.

Shortcomings Of Using A 2D

Array For A Matrix

  • Indexes are off by 1. • Java arrays do not support matrix operations

such as add, transpose, multiply, and so on.^ – Suppose that x and y are 2D arrays. Can’t do x + y,

x –y, x * y, etc. in Java.

  • Develop a class Matrix for object-oriented

support of all matrix operations. See text.

Diagonal Matrix

  • x(i,j) is on diagonal iff i = j • number of diagonal elements in an

n x n matrix is n

  • non diagonal elements are zero • store diagonal only vs n

2

whole

An n x n matrix in which all nonzero terms are either

on or below the diagonal.

  • x(i,j) is part of lower triangle iff i >= j.• number of elements in lower triangle is 1 + 2 +

… + n = n(n+1)/2.

  • store only the lower triangle

Lower Triangular Matrix