Handling array size limitations - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Handling array size limitations

Description:

Don't always know what size to allocate at start ... Absolutely limits the size of the problem not a good idea ... of common methods like add, size, iterator ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 13
Provided by: michaelc7
Category:

less

Transcript and Presenter's Notes

Title: Handling array size limitations


1
Handling array size limitations
  • Issue array size is fixed after construction
  • Dont always know what size to allocate at start
  • Solutions (besides class ArrayList coming soon)
  • Allocate way more than enough
  • Absolutely limits the size of the problem not a
    good idea
  • Create new, larger array, and copy values
  • if (dataSize gt data.length)
  • int newData new int2 data.length
  • ... // here deep copy up to (data.length
    1)
  • data newData // copy reference (discard
    old array)

2
More array techniques
  • Finding a value
  • int i 0, target (some number)
  • boolean found false
  • while (i lt x.length !found)
  • if (xi target) found true
  • else i
  • if (found) ... // know target is at xi
  • else ... // know target is not in x
  • Removing an element 2 cases
  • 1. If order doesnt matter, replace removed item
    with last item
  • 2. Otherwise, must move all trailing items
    forward one slot
  • Inserting an element same two basic cases in
    reverse

3
Arrays of objects
  • Arrays of objects require 3 steps to use
  • Rectangle boxes // 1. declare array of
    references
  • boxes new Rectangle3 // 2. instantiate array
  • // 3. instantiate each object in the array
  • for (int i0 iltboxes.length i)
  • boxesi new Rectangle(5,5,5,5)
  • Infinite applications
  • Imagine Car myFleet ...
  • Then for(...) myFleeti.draw(g)
  • An alternative to parallel arrays
  • i.e., not name100, scores100,10, grade100
  • Better Student100 class Student name,
    scores10, grade

4
Arrays of arrays
  • Arrays store anything, including arrays!
  • Not exactly multidimensional, but workable
  • e.g., int table new int104
  • A table of integers, with 10 rows and 4 columns
  • table.length is 10
  • Each tablei.length is 4, for all i
  • Component array sizes can vary
  • table2 new int6 // now 3rd row has 6
  • Typically use nested for loops to process
  • See TicTacToe.java (p. 299)

5
java.util.ArrayList
  • An array-like data structure
  • Fill with add method adds element to end
  • Size is not fixed (grows dynamically as
    necessary)
  • Also an insert method inserts element anywhere
  • Specify position 0..size (like arrays) where
    element goes
  • Use set and get methods to change and access
  • Cannot use or notation like arrays
  • New with Java 5 is a generic class
  • Means type of elements are specified insures
    all are same type
  • Before Java 5 (so still available) element type
    is Object
  • So can store anything in a ArrayList, but must
    cast before use
  • Also, compiler had no way to verify correct type

6
How to use ArrayLists
  • Declare/create ArrayList (no need to size it)
  • ArrayList a new ArrayList()
  • Or with Java 5 can specify the type
  • ArrayListltTgt a new ArrayListltTgt()
  • // where T is an object type not a primitive
    data type
  • Add objects to end, or set and get specific
    objects
  • ArrayListltRectanglegt a new ArrayListltRectanglegt(
    )
  • a.add(new Rectangle(5,5,5,5))
  • Rectangle r a.get(0) // gets first
  • a.set(0, new Rectangle(0,0,10,10)) // replaces
    first
  • Simple insert and remove too
  • a.insert(i, new Rectangle(1,1,1,1)) // inserts
    in position i
  • a.remove(i) // removes element in position i

7
ArrayList and primitive types
  • Must use wrapper classes for primitive data
    types
  • Byte, Short, Integer, Long, Float, Double,
    Character, Boolean
  • E.g., to store double values in list
  • ArrayListltDoublegt list new ArrayListltDoublegt()
  • list.add( new Double(17.64) ) // what really
    happens
  • list.add( 0.74 ) // what Java 5 autoboxing
    feature allows
  • Convert back to primitive type on retrieval
  • double d list.get(0).doubleValue() // what
    really happens
  • double d list.get(0) // with Java 5
    auto-unboxing feature
  • Note additional step if type of list not
    specified
  • double d ((Double)list.get(0)).doubleValue()

8
More java.util collections
  • List actually an interface
  • Defines a set of common methods like add, size,
    iterator
  • Shared by ArrayList, LinkedList, and others
  • Note Collections methods to manipulate List
    objects
  • Collections.shuffle(list) // randomly shuffles
    the list
  • Collections.sort(list) // assuming items are
    Comparable
  • Stack a LIFO (last in, first out) data
    structure
  • StackltStringgt s new StackltStringgt()
  • s.push(dog) ... // push objects onto top of
    stack
  • while (!s.isEmpty())
  • ... s.pop() // removes/returns top
    object
  • Also trees, sets, hash tables,

9
Modularity
  • Also a structured programming topic
  • Can replace a rectangle with a module
  • Modules contain stacked/nested structures
  • Java modules
  • methods (the most basic modular units)
  • classes (collections of related methods)
  • packages (collections of related classes)

10
Using methods invoking
  • Direct translation of algorithm e.g.,
  • getData()
  • process()
  • showResults()
  • In turn, the method process() might do
  • result calculate(x, y)
  • where calculate is another method, one that
    returns a value based on x and y.
  • And so on

11
Note parameters are copies
  • e.g., void foo(int x)
  • x 5 // changes copy of the value
    passed
  • So what does the following code print?
  • int a 1
  • foo(a)
  • System.out.print(a a)
  • Answer a 1
  • Is it the same for references? Consider the
    following
  • String s APPLE
  • anyMethod(s)
  • System.out.print(s) // prints APPLE
  • Read advanced topic 9.1, pp. 334-335

12
But references are references
  • A reference is used to send messages to an object
  • e.g., void foo(Rectangle x)
  • x.translate(5,5)
  • // actually moves the rectangle
  • Copy of reference is just as useful as the
    original
  • i.e., although methods cannot change a reference,
    they can change the original object
  • But only if the object is mutable
  • Not an issue with immutable types a class with
    no set methods like Strings
Write a Comment
User Comments (0)
About PowerShow.com