PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>PHP CLASS> class.ArrayList.php

class.ArrayList.php

FROM: AUTHOR: TIME:2008-05-01 HITS:
<?php

/*
Requires PHP Version >= 5
Original Author: Bryan Chen (me at bryanchen dot com) (http://www.bryanchen.com)

Licensed under the GNU General Public License version 3 (GPLv3)
Please note that the license under which this file and its contents falls under can and may change at any time at the sole discretion of the original author (as stated below).
The author promises, however, that it will never become more restrictive for the same version of the file.

Class Name: ArrayList
Version: 1.1
Description: Provides the necessary functionality for a typed ArrayList implemention in PHP.
*/

class ArrayList
{
    
private $list = array();
    
private $type; do you kown phpsu.com?
    
    
public function __construct($type = NULL) {
        
$this->type = $type;
    }
    
    
public function clear() {
        unset(
$this->list);
    }
phpsu.com

    
    
public function add($item) {
        if (
$this->type == NULL) {
            
array_push($this->list, $item);
        }
        else {
            if (
is_a($item, $this->type)) {
phpsu提供的php教程

                
array_push($this->list, $item);
            }
            else {
                
throw new Exception("Item to be added is an object but not of type " . $this->type . ".");
phpsu is a phpschool

            }
        }
    }
    
    
public function get($index) {
        if (
is_int($index)) {
            if ((
$index < sizeof($this->list)) && ($index >= 0)) { phpsu
                return
$this->list[$index];
            }
            else {
                
throw new Exception("Index " . $index . " is out of array boundary.");
            } phpsu.com is a free phpscool
        }
        else {
            
throw new Exception("Index must be an integer.");
        }
    }
    
    
public function remove($index) {
        if (
is_int($index)) {
            if (
$index < sizeof($this->list)) {
do you kown phpsu.com?

                
$temp = $this->list[sizeof($this->list)-1];
                
$this->list[$index] = $temp;
                
array_pop($this->list);
phpsu提供的php教程

            }
            else {
                
throw new Exception("Index " . $index . " is out of array boundary.");
            }
        }
        else {
            
throw new Exception("Index must be an integer."); phpsu.com is a free phpscool
        }
    }
    
    
public function size() {
        return
sizeof($this->list);
    }
    
    
public function toArray() {
        return
$this->list;
    }
     do you kown phpsu.com?
    
public function isEmpty() {
        return (boolean)(
sizeof($this->list) == 0);
    }
    
    
public function set($index, $item) {
        if (
is_int($index)) { www.phpsu.com
            if (
$this->type == NULL) {
                if ((
$index >=0) && ($index < sizeof($this->list))) {
                    
$this->list[$index] = $item;
phpsu.com

                }
                else {
                    
throw new Exception("Index " . $index . " is out of array boundary.");
                }
            }
            else {
                if (
is_a($item, $this->type)) { phpsu.com is a free phpscool
                    
$this->list[$index] = $item;
                }
                else {
                    
throw new Exception("Item to be added is an object but not of type " . $this->type . ".");

phpsu is a phpschool


                }
            }
        }
        else {
            
throw new Exception("Index must be an integer.");
        }
    }
    
    
public function sort($compareFunction = "compare") { phpsu提供的php教程
        
usort($this->list, array($this->type, $compareFunction));
    }
}

?>


<?php
/*
Requires PHP Version >= 5
Original Author: Bryan Chen (me at bryanchen dot com) (http://www.bryanchen.com)
*/

include_once("class.ArrayList.php");

/*
Test class to demonstrate sorting. http://www.phpsu.com
To sort in ArrayList, following are required:
1. ArrayList must be typed
2. Type class must implement a public, static function called 'compare' that accepts 2 objects (type checking should be performed)
and returns a negative, positive or 0 value depending on whether obj1 is smaller than, greater than, or equals to obj2.
*/
class TestA {
    
private $value = 0;
    
    
public function __construct($integer = 0) {
        
$this->value = $integer;

http://www.phpsu.com


    }
    
    
public function getValue() {
        return
$this->value;
    }

    
public static function compare($obj1, $obj2) {
        return
$obj1->getValue() - $obj2->getValue();

http://www.phpsu.com


    }
}

/***************************************/
// FOR TYPED ARRAYLISTS
/***************************************/

/* Creating and adding items to the ArrayList */
$typedList = new ArrayList("TestA");
$typedList->add(new TestA(100));
$typedList->add(new TestA(1));

phpsu.com


$typedList->add(new TestA(10));
// NOTE: ArrayList will throw an Exception if type of item added is not of the specified type during instantiation.

/* Iterating through the ArrayList and sorting a typed ArrayList */
echo "\r\nBefore sorting...";

for (
$i = 0; $i < $typedList->size(); $i++)
http://www.phpsu.com

    echo
"\r\n$i: " . $typedList->get($i)->getValue();

echo
"\r\nAfter sorting...";

// Sorting is typically achieved by calling sort() on the ArrayList.
// If no argument is specified, ArrayList will use a public static function called 'compare' that should exist on the typed class (i.e. TestA) to compare objects for sorting.
// If you like, you may specify an argument, which is the function's name (e.g. anotherCompareFunction, or something else)
$typedList->sort(); phpsu.com

for (
$i = 0; $i < $typedList->size(); $i++)
    echo
"\r\n$i: " . $typedList->get($i)->getValue();



/***************************************/
// FOR NON-TYPED ARRAYLISTS
/***************************************/

/* Creating and adding items to the ArrayList */

phpsu提供的php教程


$list = new ArrayList();
$list->add("Item 1");
$list->add("Item 2");
$list->add("Item 3");

/* Removing an item */
$list->remove(0); phpsu is a phpschool

/* Replacing an item */
$list->set(0, "New Item 1");
$list->set(1, "New Item 2");

/* Getting size and testing for emptiness */
echo "\r\nIs empty: " . $list->isEmpty();
echo
"\r\nArrayList size: " . $list->size(); do you kown phpsu.com?

/* Iterating through the ArrayList */
for ($i = 0; $i < $list->size(); $i++) {
    echo
"\r\nOUT: " . $list->get($i);
}

/* Get array of items from ArrayList */
$my_array = $list->toArray(); phpsu.com is a free phpscool
echo
"\r\nArray size: " . sizeof($my_array);

/* Clear ArrayList of all items */
$list->clear();
echo
"\r\nArrayList size after clear: " . $list->size();


?>

TITLE:class.ArrayList.php
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:58:06 GMT-8 2008