JavaScript Tutorial/Dojo toolkit/ArrayList

Материал из Web эксперт
Перейти к: навигация, поиск

Clear ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("Z");
        al.add("C");
        al.add("D");
        
        al.clear();
        alert(al.toArray());
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Convert ArrayList to Array

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        al.reverse();
        alert(al.toArray());
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Create an ArrayList and add elements to it

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        alert(al);
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Does ArrayList contain

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        
        alert(al.contains("B"));      
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Get element in ArrayList by index

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        alert(al.item(2));
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Get index of an item in ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        
        alert(al.indexOf("C"));    
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Get Iterator from ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        
        var it = al.getIterator();
        while (!it.atEnd()) {
          alert(it.get());
        }        
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Insert element into an ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        al.insert(1, "L");
        alert(al);
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Remove element from ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        var o = al.item(2);
        al.remove(o);
        alert(al.toArray());
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Reverse an ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        al.reverse();
        alert(al.toArray());
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Set element by index

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        al.setByIndex(2, "M");
        alert(al);
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>


Sort an ArrayList

<html>
  <head>
    <script type="text/javascript">
      var djConfig = {
        baseScriptUri : "js/dojo/",
        parseOnLoad : true
      };
    </script>
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script>
      dojo.require("dojox.collections.ArrayList");
      function testIt() {
        var al = new dojox.collections.ArrayList();
        al.add("A");
        al.add("Z");
        al.add("C");
        al.add("D");
        
        al.sort();
        alert(al.toArray());
      }
    </script>
  </head>
  <body onLoad="testIt();">
  </body>
</html>