javascript泛型集合类

  • Post author:
  • Post category:java



1)Hash


<script type="text/javascript">
var h1 = new Hash();
h1.add(1,2);
h1.add("aa","xxx");
h1.add("b","yy");
h1.add("c","zzz");

h1.forEach(function(item,i,h){
    alert(item.key+":"+item.value+":"+Object.getTypeName(item.key));
});
</script>



2)List

<script type="text/javascript">
var lst = new List(Number)();
    lst.add(1);
    lst.add(32);
    lst.add(444);
    
    var lst2 = new List(Number)();
    lst2.add(-1);    
    lst2.insertRange(0,lst);
    lst2.forEach(function(item,i,lt){
        alert(item);
    });
    alert(lst2.getCount());
</script>



3)Dictionary

<script type="text/javascript">
var ds = new Dictionary(Number,String)();
ds.add(1,"1");
ds.add(2,"222");
ds.forEach(function(item,i,d){
    alert(Object.getTypeName(item.key)+":"+item.key+","+item.value);
});

var MyClass = function(name){
    this.name = name;
}
MyClass.__typeName = 'MyClass';
MyClass.__class = true;
var a = new MyClass("a");
var b = new MyClass("b");

var dss = new Dictionary(MyClass,String)();

dss.add(a,"x");
dss.add(b,"y");
dss.forEach(function(item,i,d){
    alert(Object.getTypeName(item.key)+":"+item.key.name+","+item.value);
});
</script>



4)Stack

<script type="text/javascript">
var sk = new Stack(Number)();
    sk.push(1);
    sk.push(321);
    sk.forEach(function(item,i,k){
        alert(item+":"+i);
    },'a');
</script>



5)Queue

<script type="text/javascript">
var qs = new Queue(String)();
qs.enqueue("1");
qs.enqueue("xxx");
qs.forEach(function(item,i,q){
    alert(item+":"+i);
});
</script>