-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arraylist.html
68 lines (68 loc) · 1.6 KB
/
Arraylist.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<html>
<head>
<title>Javascript Example</title>
<script type="text/javascript">
function ArrayList()
{
this.arr = [];
return arr;
}
ArrayList.prototype = {
add: function(number){
this.arr.push(number);
},
display: function(){
return this.arr;
},
remove: function(){
this.arr.pop();
},
get: function(index){
if(index < this.arr.length)
return this.arr[index];
else
return "No such index";
},
update: function(index,value){
if(index < this.arr.length)
{
this.arr[index] = value;
return true;
}
else
return false;
},
sort: function(){
/*var temp = 0;
for(var index = 0; index < this.arr.length - 1; index++)
for(var jindex = index + 1; jindex < this.arr.length; jindex++)
if(this.arr[index] > this.arr[jindex])
{
temp = this.arr[index];
this.arr[index] = this.arr[jindex];
this.arr[jindex] = temp;
}
//console.log(this.arr);*/
this.arr.sort();
}
}
var arr = new ArrayList();
arr.add(6);
arr.add(2);
arr.add(3);
console.log("The Number at index 2:" + arr.get(2));
console.log(arr.get(5));
console.log("The array contents: " + arr.display());
arr.remove();
console.log("The array contents: " + arr.display());
arr.add(4);
console.log("Value updation:" + arr.update(2,5));
console.log("The array contents: " + arr.display());
arr.sort();
console.log("The array contents: " + arr.display());
</script>
</head>
<body>
<h1>Welcome to Javascript</h1>
</body>
</html>