JavaScript Tutorial/Dojo toolkit/ArrayList
Версия от 18:52, 25 мая 2010; (обсуждение)
Содержание
- 1 Clear ArrayList
- 2 Convert ArrayList to Array
- 3 Create an ArrayList and add elements to it
- 4 Does ArrayList contain
- 5 Get element in ArrayList by index
- 6 Get index of an item in ArrayList
- 7 Get Iterator from ArrayList
- 8 Insert element into an ArrayList
- 9 Remove element from ArrayList
- 10 Reverse an ArrayList
- 11 Set element by index
- 12 Sort an ArrayList
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>