博客
关于我
顺序表各种功能的实现
阅读量:336 次
发布时间:2019-03-04

本文共 2548 字,大约阅读时间需要 8 分钟。

顺序表各种功能的实现

public class Demo09 {       public static void main(String[] args) {           MyArrayList myArrayList = new MyArrayList();        myArrayList.add(0,1);        myArrayList.add(1,2);        myArrayList.add(1,3);        myArrayList.add(2,4);        myArrayList.show();//打印顺序表        myArrayList.add(3,99);        myArrayList.show();        System.out.println(myArrayList.key(99));//判断是否有key元素,true为有        System.out.println(myArrayList.keyNum(99));//输出key元素的位置        System.out.println(myArrayList.getPos(3));//输出pos下标的元素        myArrayList.replace(3,88);//替换pos位置的元素为val        myArrayList.show();        myArrayList.del(88);//删除第一次出现的关键字key        myArrayList.show();        System.out.println(myArrayList.getLegth());//获取长度        myArrayList.clear();//清空顺序表        myArrayList.show();    }}class MyArrayList{       //普通成员变量    public int[] elem;    public int usedSize;    //构造方法    public MyArrayList(){           this.elem = new int[1];    }    //判断是否有key元素    public boolean key (int key){           for (int i = 0 ; i < this.usedSize; i++ ){               if(elem[i] == key){                   return true;            }        }        return false;    }    //输出key元素的下标    public int keyNum (int key){           for (int i = 0 ; i < this.usedSize; i++ ){               if(elem[i] == key){                   return i;            }        }        return -1;    }    //获取pos下标位置的元素    public int getPos(int pos){           if (pos<0||pos>=this.usedSize) {               return -1;        }        return this.elem[pos];    }    //替换pos位置的元素为val    public void replace(int pos,int val){           if (pos<0||pos>=this.usedSize) {               System.out.printf("pos不在范围内");        }else {               this.elem[pos] = val;        }    }    //删除第一次出现的关键字key    public void del (int key){           for (int i = 0; i < this.usedSize; i++) {               if(elem[i] == key){                   for (;i
this.usedSize || pos<0){ return; } for (int i = this.usedSize-1;i >= pos ;i--){ elem[i+1] = elem[i]; } this.elem[pos] = data; this.usedSize++; } //扩容 public void expand(){ int[] tmp = new int[this.elem.length*2]; for (int i = 0 ;i < this.elem.length ; i++ ){ tmp[i] = this.elem[i]; } this.elem = tmp ; return ; } //打印顺序表 public void show(){ for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); }}

转载地址:http://yzbe.baihongyu.com/

你可能感兴趣的文章
Nexus指南已经发布
查看>>
NFinal学习笔记 02—NFinalBuild
查看>>
NFS
查看>>
NFS Server及Client配置与挂载详解
查看>>
NFS 服务配置篇
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS服务器配置-服务启动与停止
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>