ics202 data structures. class array { protected object[] data; protected int base; public array (int...

4
Ics202 Data Structures

Upload: arlene-carpenter

Post on 02-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public

Ics202Data Structures

Page 2: Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public

class Array { protected Object[] data; protected int base;

public Array (int n, int m) {

data = new Object[n];base = m; }

public Array (){ this (0, 0); }

public Array (int n){ this (n, 0); }

public void assign (Array array) {if (array != this){

if (data.length != array.data.length)data = new Object [array.data.length];

for (int i = 0; i < data.length; ++i)data [i] = array.data [i];

base = array.base; } }

Java Class

Page 3: Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public

public Object[] getData (){ return data; }

public int getBase (){ return base; }

public int getLength (){ return data.length; }

public Object get (int position){ return data [position - base]; }

public void put (int position, Object object){ data [position - base] = object; }

public void setBase (int base){ this.base = base; }

public void setLength (int newLength) {if (data.length != newLength) {

Object[] newData = new Object[newLength]; int min = data.length < newLength ?

data.length : newLength; for (int i = 0; i < min; ++i)

newData [i] = data [i]; data = newData; }}}

Page 4: Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public

class Arraytest{public static void main(String [] args) { // define an array a of length 15 and base 5 // assign the base of the array a to b // assign the length of the array a to l

// display the base and the length of a

// initialize the elements of the array a

// display the elements of the array a

// define a second array x of length 15 and base 5

// assign the elements of a into x // display the elements of the array x

// modify the base of the array a} }