Foundational Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Foundational Data Structures

Description:

In Java an array is an object that contains a collection of objects, all of the same type. ... length attribute gives the number of elements in an array. ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 19
Provided by: bina1
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Foundational Data Structures


1
Foundational Data Structures
  • B.Ramamurthy
  • Chapter 4

2
Introduction
  • Array and linked list are the base on which many
    other Abstract Data Types (ADTs) are built.
  • Arrays and linked list are called the
    foundational data structures.
  • In this discussion we will examine the support
    for arrays in Java language and a number of
    linked list implementation alternatives.

3
Arrays
  • In Java an array is an object that contains a
    collection of objects, all of the same type.
  • int a new int 5
  • allocates 5 integers and assigns it to array
    variable a.

4
Array internal representation
length attribute gives the number of elements in
an array. sizeof(arrayname) gives the storage
needed for the array elements (not including the
length attribute)
Array object
6
length
5
Array Initialization
  • At the time of declaration an array can be
    initialized to a set of values.
  • Example
  • protected final static String courses
    CSE115, CSE116, CSE191, CSE191,
    CSE241, CSE250, CSE305, CSE341,
    CSE396,CSE351, CSE421
  • protected JLabel reg1Labels

6
Length Attribute
  • Often we want to find the length of a certain for
    processing purposes.
  • Use length attribute (or property)
  • for (int j 0 j lt reg1Labels.length j)
  • regLabelsj new JLabel(coursesj)
  • Creates a Jlabel with course names as values.

7
Array Wrapper Class
  • Wrapper class is concept is often used when a
    built-in class does not satisfy the needs of a
    development environment.
  • Lets examine how to build a Wrapper class Array.
  • Class data, constructors, assign method,
    accesors, mutators (resizers), toString and equal.

8
Constructors
  • public class Array
  • //data
  • protected Object data
  • protected int base
  • // constructors
  • public Array (int n, int m)
  • data new Objectn
  • base m
  • public Array() this(0,0)
  • public Array (int n) this (n,0)
  • // other methods

9
Assign method
  • public void assign(Array array)
  • if (array ! this)
  • if (data.length ! array.data.length)
  • data new Objectarray.data.length
  • for (int j 0 j lt data.length j)
  • data j array.dataj
  • base array.base

10
Accessor (methods)
  • public Object getData()
  • return data // is this good style?
  • public int getBase()
  • return base
  • public int getLength()
  • return data.length // is this right?

11
get and put methods
  • public Object get (int position )
  • return data position -base
  • public void put (int position, Object object)
  • data position - base object

12
setLength and setBase
  • public void setBase (int base)
  • this.base base
  • public void setLength (int newLength)
  • if (data.length ! newLength)
  • Object newData new ObjectnewLength
  • int min data.lengthlt newLength?
    data.length newLength
  • for (int j 0 j lt min j)
  • newData j dataj
  • data newData

13
Picture It
Array b new Array(6,3)
external
3 4 5 6 7 8
Therefor, actual index is (position - base)
And so on...
internal
14
toString method
  • public String toString()
  • StringBuffer buf new StringBuffer()
  • for(int j 0 j lt data.length j)
  • buf.append(String.valueOf(dataj)
  • return buf.toString()

15
equal method
  • public boolean equal(Array other)
  • if (data.length ! other.data.length)
  • return false
  • else
  • int j 0
  • while ((j lt data.length) (dataj
    other.dataj))
  • j
  • if (j data.length) return true
  • else return false

16
Multi-dimensional Array
  • A multi-dimensional array of dimension n is a
    collection of items which is accessed via
    n-subscript expressions.
  • Java language actually supports array of arrays.
  • Example
  • int x new int35

17
Two dimensional Array Manipulation
  • 1. Declare two dimensional array of integers of 4
    rows and 4 columns.
  • 2. Initialize the array to all 1s.
  • 3. Update the cell contents of the array to the
    sum of the row and column index.
  • 4. Make all the diagonal elements 0.
  • 5. Flip the contents of row 2 with column 3.

18
Other Variations of Array
  • Since Java supports multi-dimensional arrays as
    array of arrays, other shapes such as triangular
    arrays are possible.
  • For example how will you declare this?
  • 0,0
  • 1,01,1
  • 2,02,12,2
  • 3,03,13,23,3
Write a Comment
User Comments (0)
About PowerShow.com