Docsity

Data Representation Methods - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Data Representation Methods - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Data Representation Methods

array --- Chapter 5

linked --- Chapter 6

simulated pointer --- Chapter 7

Linear List Array Representation

use a one-dimensional array element[]

a b c d e

L = (a, b, c, d, e)

Store element i of list in element[i].

Mapping That Skips Every Other

Position

a b c d e

Wrap Around Mapping

d e a b c

Add/Remove An Element

a b c d e

size = 5

a g b c d e

size = 6

add(1,g)

Data Type Of Array element[]

Data type of list elements is unknown.

Define element[] to be of data type Object.

Cannot put elements of primitive data types

(int, float, double, char, etc.) into our linear

lists.

Increasing Array Length Length of array element[] is 6. a b c d e f newArray = new Object[15]; First create a new and larger array

Increasing Array Length Now copy elements from old array to new one. a b c d e f a b c d e f

Altogether Now

// create a new array of proper length and data type

Object [] newArray = new Object [newLength];

// copy all elements from old array into new one

System.arraycopy(element, 0, newArray, 0,

element.length);

// rename array

element = newArray;

public static Object [] changeLength(Object [] a, int newLength) { Object [] newArray = new Object [newLength]; System.arrayCopy(โ€ฆ); return newArray; } Integer [] a = new Integer [10]; โ€ฆ. a = (Integer []) changeLength(a, 100); // erroneous

Space Complexity newArray = new char[7]; space needed = 2 * newLength โ€“ 1 = 2 * maxListSize โ€“ 1 element[6] a b c d e f

Array Doubling Double the array length. a b c d e f newArray = new char[12]; a b c d e f Time for n adds goes up by Theta(n). Space needed = 1.5newLength. Space needed <= 3maxListSize โ€“ 3

How Big Should The New Array Be? Resizing by an additive constant c requires at most (maxListSize โ€“ 1) + (maxListSize โ€“ 1 + c) = 2 * (maxListSize โ€“ 1) + c space. Resizing by any constant factor new length = c * old length requires at most (1+c) * (maxListSize -1) space_._

What Does Java Do?

java.util.Vector โ€ฆ array doubling

java.util.ArrayList โ€ฆ c = 1.

dataStructures.ArrayLinearList of text โ€ฆ c = 2