Classes:
Collections
Arrays
ArrayList
SortedList extends ArrayList
HashMap
HashSet
*/
/****************
Collections
NOTE:sort() return a new List
****************/
function Collections(){}
Collections.sort=function(){
if(arguments.length==1){
var s=new SortedList();
s.addAll(arguments[0]);
return s;
}
else if(arguments.length==2){
var s=new SortedList();
s.setComparator(arguments[1]);
s.addAll(arguments[0]);
return s;
}
else
throw "IllegalArgument";
}
/***************
Arrays
****************/
function Arrays(){}
Arrays.asList=function(arr){
return new ArrayList(arr);
}
//ListIterator
function ListIterator(table,len){
this.table=table;
this.len=len;
this.index=0;
this.hasNext=function() {
return this.index }
this.next=function() {
if(!this.hasNext())
throw "No such Element!";
return this.table[this.index++];
}
}
/********************
ArrayList
********************/
function ArrayList(){
this.buffer=new Array();
if(arguments.length>0) this.buffer=arguments[0];
this.length=this.buffer.length;
}
ArrayList.prototype.hashCode=function(){
var h=0;
for(var i=0;i
return h;
}
ArrayList.prototype.size=function(){
return this.length;
}
ArrayList.prototype.clear=function(){
for(var i=0;i
this.length=0;
}
ArrayList.prototype.isEmpty=function(){
return this.length==0;
}
ArrayList.prototype.toArray=function(){
var copy=new Array();
for(var i=0;i
}
return copy;
}
ArrayList.prototype.get=function(index){
if(index>=0 && index
return null;
}
ArrayList.prototype.remove=function(param){
var index=0;
if(isNaN(param)){
index=this.indexOf(param);
}
else index=param;
if(index>=0 && index
this.length-=1;
return true;
}
else return false;
}
ArrayList.prototype.add=function(){
var args=arguments;
if(args.length==1){
this.buffer[this.length++]=args[0];
return true;
}
else if(args.length==2){
var index=args[0];
var obj=args[1];
if(index>=0 && index for(var i=this.length;i>index;i--)
this.buffer[i]=this.buffer[i-1];
this.buffer[i]=obj;
this.length+=1;
return true;
}
}
return false;
}
ArrayList.prototype.indexOf=function(obj){
for(var i=0;i
}
return -1;
}
ArrayList.prototype.lastIndexOf=function(obj){
for(var i=this.length-1;i>=0;i--){
if(this.buffer[i].equals(obj)) return i;
}
return -1;
}
ArrayList.prototype.contains=function(obj){
return this.indexOf(obj)!=-1;
}
ArrayList.prototype.equals=function(obj){
if(this.size()!=obj.size()) return false;
for(var i=0;i
}
return true;
}
ArrayList.prototype.addAll=function(list){
var mod=false;
for(var it=list.iterator();it.hasNext();){
var v=it.next();
if(this.add(v)) mod=true;
}
return mod;
}
ArrayList.prototype.containsAll=function(list){
for(var i=0;i
}
return true;
}
ArrayList.prototype.removeAll=function(list){
for(var i=0;i
}
}
ArrayList.prototype.retainAll=function(list){
for(var i=this.length-1;i>=0;i--){
if(!list.contains(this.buffer[i])){
this.remove(i);
}
}
}
ArrayList.prototype.subList=function(begin,end){
if(beginif(end>this.length) end=this.length;
var newsize=end-begin;
var newbuffer=new Array();
for(var i=0;i
}
return new ArrayList(newbuffer);
}
ArrayList.prototype.set=function(index,obj){
if(index>=0 && index
this.buffer[index]=obj;
return temp;
}
}
ArrayList.prototype.iterator=function iterator(){
return new ListIterator(this.buffer,this.length);
}
/*****************************
SortedList extends ArrayList
*****************************/
function SortedList(){
this.com=null;
}
SortedList.prototype=new ArrayList();
SortedList.prototype.setComparator=function(comp){
if(this.length!=0) throw "Only can be set when list is empty";
this.com=comp;
}
SortedList.prototype.getComparator=function(){
return this.com;
}
//override
SortedList.prototype.add=function(obj){
var index = this.indexOf(obj);
for(var i=this.length;i>index;){
this.buffer[i]=this.buffer[--i];
}
this.buffer[index]=obj;
this.length++;
}
//override
SortedList.prototype.indexOf=function(obj){
if(this.length==0) return 0;
var min=0,max=this.length-1;
var mid=0;
while(min
mid = (min+max) >> 1;
var c=0;
if(this.com==null) c=obj.compareTo(this.buffer[mid]);
else c=this.com.compare(obj,this.buffer[mid]);
if(c==0){
return mid;
}
else if(c max=mid-1;
}
else{
min=mid+1;
}
}
mid =(min+max) >>1;
return mid+1;
}
//override
SortedList.prototype.contains=function(obj){
if(this.length==0) return false;
var min=0,max=this.length-1;
var mid=0;
while(min mid = (min+max) >> 1;
var c=0;
if(this.com==null) c=obj.compareTo(this.buffer[mid]);
else c=this.com.compare(obj,this.buffer[mid]);
if(c==0){
return true;
}
else if(c max=mid-1;
}
else{
min=mid+1;
}
}
return false;
}
//override
SortedList.prototype.subList=function(begin,end){
var sl=new SortedList();
s1.setComparator(this.com);
var sub=ArrayList.prototype.subList(begin.end);
sl.addAll(sub);
return sl;
}
/****************************
HashMap
****************************/
function Entry(h,k,v,n){
this.value = v;
this.next = n;
this.key = k;
this.hash = h;
this.getKey=function(){
return this.key;
}
this.getValue=function() {
return this.value;
}
this.setValue=function(newValue) {
var oldValue = this.value;
this.value = newValue;
return oldValue;
}
this.equals=function(o){
var e = o;
var k1 = this.getKey();
var k2 = e.getKey();
var v1 = this.getValue();
var v2 = e.getValue();
return (k1.equals(k2) && v1.equals(v2));
}
this.hashCode=function() {
return this.key.hashCode() ^ this.value.hashCode();
}
this.toString=function() {
return this.getKey() + "=" + this.getValue();
}
}
function HashIterator(table,index,ne){
this.table=table;
this.ne=ne;
this.index=index;
this.current=null;
this.hasNext=function() {
return this.ne != null;
}
this.next=function() {
var e = this.ne;
if (e == null)
throw "No such Element";
var n = e.next;
var t = this.table;
var i = this.index;
while (n == null && i > 0)
n = t[--i];
this.index = i;
this.ne = n;
this.current=e;
return this.current;
}
}
function HashMap()
{
this.len=8;
this.table=new Array();
this.length=0;
}
// refer to java.util.HashMap
HashMap.hash=function(x){
var h = x.hashCode();
h += ~(h h ^= (h >>> 14);
h += (h h ^= (h >>> 10);
return h;
}
HashMap.prototype.rehash=function(){
var oldTable = this.table;
this.table=new Array();
//transfer
for (var i = 0; i var e = oldTable[i];
if (e != null) {
oldTable[i] = null;
do {
var next = e.next;
var j = this.indexFor(e.hash);
e.next = this.table[j];
this.table[j] = e;
e = next;
} while (e != null);
}
}
}
HashMap.prototype.indexFor=function(h) {
var index= h & (this.len-1);
return index;
}
HashMap.prototype.size=function() {
return this.length;
}
HashMap.prototype.isEmpty=function() {
return this.length == 0;
}
HashMap.prototype.get=function(key) {
var hash =HashMap.hash(key);
var i = this.indexFor(hash);
var e = this.table[i];
while (true) {
if (e ==null)
return null;
if (e.hash == hash && key.equals(e.key))
return e.value;
e = e.next;
}
}
HashMap.prototype.containsKey=function(key) {
var hash =HashMap.hash(key);
var i = this.indexFor(hash);
var e = this.table[i];
while (e != null) {
if (e.hash == hash && key.equals(e.key))
return true;
e = e.next;
}
return false;
}
HashMap.prototype.put=function(key,value) {
var hash = HashMap.hash(key);
var i = this.indexFor(hash);
for (var e = this.table[i]; e != null; e = e.next) {
if (e.hash == hash && key.equals(e.key)) {
var oldValue = e.value;
e.value = value;
return oldValue;
}
}
this.addEntry(hash, key, value, i);
var r=Math.ceil(this.length * 1.5);
if(r > this.len){
this.len= this.len this.rehash();
}
return null;
}
HashMap.prototype.putAll=function (map){
var mod=false;
for(var it=map.iterator();it.hasNext();){
var e=it.next();
if(this.put(e.getKey(),e.getValue())) mod=true;
}
}
HashMap.prototype.remove=function(key) {
var e = this.removeEntryForKey(key);
return (e ==null ? null : e.value);
}
HashMap.prototype.removeEntryForKey=function(key) {
var hash = HashMap.hash(key);
var i = this.indexFor(hash);
var prev = this.table[i];
var e = prev;
while (e != null) {
var next = e.next;
if (e.hash == hash && key.equals(e.key)) {
this.length--;
if (prev.equals(e))
this.table[i] = next;
else
prev.next = next;
return e;
}
prev = e;
e = next;
}
return e;
}
HashMap.prototype.clear=function() {
for (var i = 0; i this.table[i] = null;
this.length = 0;
}
HashMap.prototype.containsValue=function(value) {
if (value == null) return false;
var tab = this.table;
for (var i = 0; i for (var e = tab[i] ; e != null ; e = e.next)
if (value.equals(e.value))
return true;
return false;
}
HashMap.prototype.addEntry=function(hash, key, value, bucketIndex) {
this.table[bucketIndex] = new Entry(hash, key, value, this.table[bucketIndex]);
this.length++;
}
HashMap.prototype.iterator=function(){
var i=this.table.length;
var next=null;
while(i>0 && next==null){
next=this.table[--i];
}
return new HashIterator(this.table,i,next);
}
HashMap.prototype.hashCode=function(){
var h=0;
for(var it=this.iterator();it.hasNext();){
h+=it.next().hashCode();
}
return h;
}
HashMap.prototype.equals=function(map){
if(!this.typeMatches(map)) return false;
if(map.size()!=this.size()) return false;
for(var it=this.iterator();it.hasNext();){
var e=it.next();
var key=e.getKey();
var value=e.getValue();
if(!value.equals(map.get(key))) return false
}
return true;
}
/*************************
HashSet
**************************/
function HashSetIterator(ite){
this.it=ite;
this.hasNext=function() {
return this.it.hasNext();
}
this.next=function() {
return this.it.next().getKey();
}
}
function HashSet(){
this.map=new HashMap();
}
HashSet.NULL=new Number("!THIS IS NULL!");
HashSet.prototype.size=function(){
return this.map.size();
}
HashSet.prototype.isEmpty=function() {
return this.map.isEmpty();
}
HashSet.prototype.contains=function(o) {
return this.map.containsKey(o);
}
HashSet.prototype.add=function(o){
return this.map.put(o,HashSet.NULL)==null;
}
HashSet.prototype.addAll=function(set){
var mod=false;
for(var it=set.iterator();it.hasNext();){
if(this.add(it.next())) mod=true;
}
return mod;
}
HashSet.prototype.remove=function(o) {
return this.map.remove(o).equals(HashSet.NULL);
}
HashSet.prototype.clear=function() {
this.map.clear();
}
HashSet.prototype.iterator=function(){
return new HashSetIterator(this.map.iterator());
}
HashSet.prototype.equals=function(o) {
if(!this.typeMatches(o)) return false;
if (o.size() != this.size()) return false;
for(var it=this.iterator();it.hasNext();){
if(!o.contains(it.next())) return false;
}
return true;
}
HashSet.prototype.hashCode=function() {
var h=0;
for(var it=this.iterator();it.hasNext();){
h+=it.next().hashCode();
}
return h;
}
HashSet.prototype.toArray=function(){
var arr=new Array();
var i=0;
for(var it=this.iterator();it.hasNext();){
arr[i++]=it.next();
}
return arr;
}
0
0
相关文章
javascript对象的概念是什么_如何创建和访问对象的属性和方法【教程】
javascript的测试框架jest如何使用_如何为函数和组件编写单元测试【教程】
什么是JavaScript对象_如何访问对象的属性和方法
javascript如何实现数据绑定_现代框架有何不同
javascript的React是什么_其组件化思想如何运作
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
在拼多多上赚钱主要可以通过无货源模式一件代发、精细化运营特色店铺、参与官方高流量活动、利用拼团机制社交裂变,以及成为多多进宝推广员这5种方法实现。核心策略在于通过低成本、高效率的供应链管理与营销,利用平台社交电商红利实现盈利。
28
2026.01.26
在Edge浏览器中设置主页,请依次点击右上角“...”图标 > 设置 > 开始、主页和新建标签页。在“Microsoft Edge 启动时”选择“打开以下页面”,点击“添加新页面”并输入网址。若要使用主页按钮,需在“外观”设置中开启“显示主页按钮”并设定网址。
8
2026.01.26
苹果官方查询网站主要通过 checkcoverage.apple.com/cn/zh/ 进行,可用于查询序列号(SN)对应的保修状态、激活日期及技术支持服务。此外,查找丢失设备请使用 iCloud.com/find,购买信息与物流可访问 Apple (中国大陆) 订单状态页面。
31
2026.01.26
NPD(Narcissistic Personality Disorder)即自恋型人格障碍,是一种心理健康问题,特点是极度夸大自我重要性、需要过度赞美与关注,同时极度缺乏共情能力,背后常掩藏着低自尊和不安全感,影响人际关系、工作和生活,通常在青少年时期开始显现,需由专业人士诊断。
3
2026.01.26
关闭Windows安全中心(Windows Defender)可通过系统设置暂时关闭,或使用组策略/注册表永久关闭。最简单的方法是:进入设置 > 隐私和安全性 > Windows安全中心 > 病毒和威胁防护 > 管理设置,将实时保护等选项关闭。
5
2026.01.26
铁路12306提供起售时间查询、起售提醒、购票预填、候补购票及误购限时免费退票五项服务,并强调官方渠道唯一性与信息安全。
35
2026.01.26
以工资薪金所得为例,应纳税额 = 应纳税所得额 × 税率 - 速算扣除数。应纳税所得额 = 月度收入 - 5000 元 - 专项扣除 - 专项附加扣除 - 依法确定的其他扣除。假设某员工月工资 10000 元,专项扣除 1000 元,专项附加扣除 2000 元,当月应纳税所得额为 10000 - 5000 - 1000 - 2000 = 2000 元,对应税率为 3%,速算扣除数为 0,则当月应纳税额为 2000×3% = 60 元。
12
2026.01.26
oppo云服务https://cloud.oppo.com/可以在云端安全存储您的照片、视频、联系人、便签等重要数据。当您的手机数据意外丢失或者需要更换手机时,可以随时将这些存储在云端的数据快速恢复到手机中。
40
2026.01.26
网页端充值步骤:打开浏览器,输入https://www.douyin.com,登录账号;点击右上角头像,选择“钱包”;进入“充值中心”,操作和APP端一致。注意:切勿通过第三方链接、二维码充值,谨防受骗
7
2026.01.26
热门下载
精品课程
共12课时 | 0.6万人学习
共4课时 | 0.2万人学习
共16课时 | 0.9万人学习
最新文章
