最近又重新在看慕课网的数据结构,然后把示例代码整理一下。

public class Array<E> {

    private E[] data;
private int count = 0; public Array(int length){ data = (E[]) new Object[length];
} public Array(){ this(10);
} /**
* 获取数组长度
* @return
*/
public int getLength(){ return data.length;
} public int getCount(){ return count;
} public boolean isEmpty(){ return count == 0;
} /**
* 追加元素
* @param item
*/
public Array<E> add(E item){ return add(count, item); } /**
* 插入元素
* @param index 索引位置
* @param item 元素
*/
public Array<E> add(int index, E item){ //元素索引必须合法,则插入位置不能造成元素位置不连续
if(index < 0 || index > count) throw new IllegalArgumentException("Get failed Index is illegal."); if(count == data.length){
resize((int) (data.length * 1.5));
} //插入时,从尾部开始,每个元素后移动一位,直到当前索引位置为止
for(int i = count - 1; i >= index; i--){
data[i + 1] = data[i];
} data[index] = item;
count++; return this;
} /**
* 获取元素
* @param index 索引,负数表示倒数
* @return
*/
public E get(int index){ if(index < 0) index += count;
if(index >= count) throw new IllegalArgumentException("Get failed Index is illegal."); return data[index];
} /**
* 修改元素
* @param index 索引,负数表示倒数
* @param item
*/
public Array<E> set(int index, E item){ if(index < 0) index += count;
if(index >= count) throw new IndexOutOfBoundsException();
data[index] = item; return this;
} /**
* 删除给定索引位置的元素
* @param index
* @return 元素
*/
public E remove(int index){ if(index < 0) index += count;
if(index >= count) throw new IndexOutOfBoundsException(); for(int i = index; i < count - 1; i++){
data[i] = data[i + 1];
} count--;
data[count] = null; //收缩容量
if(count == data.length / 4 && data.length / 2 > 0){
resize(data.length / 2);
} return data[index];
} /**
* 删除给定元素
* @param item
* @return
*/
public boolean remove(E item){ int index = find(item);
if(index >= 0){
remove(index);
return true;
}
return false;
} /**
* 删除所有给定元素
* @param item
* @return boolean
*/
public boolean removeAll(E item){ int index = find(item);
if(index > -1){ do{
remove(index);
index = find(item); }while(index <= 0); return true;
} return false;
} /**
* 重新分配数组长度
* @param length
*/
private void resize(int length){ E[] data = (E[]) new Object[length];
for(int i = 0; i < count; i++){
data[i] = this.data[i];
}
this.data = data;
} /**
* 判断元素是否存在
* @param item
* @return boolean
*/
public boolean contains(E item){ return (find(item) > -1)? true : false;
} /**
* 查找元素所在位置
* @param item
* @return 索引位置
*/
public int find(E item){ for(int i = 0; i < count; i++){
//对象之间用 equals 方法,表示值比较
if(data[i].equals(item)) return i;
}
return -1;
} @Override
public String toString(){ StringBuilder sb = new StringBuilder();
sb.append(String.format("Array[%d]: ", data.length, count));
sb.append('[');
for(int i = 0; i < count; i++){
sb.append(data[i]);
if(i != count - 1) sb.append(", ");
}
sb.append("]"); return sb.toString();
} }

最新文章

  1. 【JavaScript】js数组操作,由push到那么多
  2. [转]九个Console命令,让js调试更简单
  3. 如何在MFC中创建非矩形button
  4. 【python】入门学习(八)
  5. NYOJ题目64鸡兔同笼
  6. Response.Clear()和Response.ClearContent()区别
  7. Oracle创建定时器
  8. 360wifi 在 windows server 2008 / 2003 的使用方法
  9. ali面试点滴
  10. AOJ 0525 - Osenbei
  11. 北大ACM(POJ1009-Edge Detection)
  12. @import————————css代码内部链接另外css
  13. ActiveX,ATL和COM技术
  14. Windows下如何建立以&quot;.&quot;开头的文件夹
  15. 无法启动此程序因为计算机中丢失msvcr71
  16. RecyclerView下拉刷新上拉加载(三)—对Adapter的封装
  17. Puppeteer 截图及相关问题
  18. apache beam ElasticSearchIO 遇到异常后job中断执行 自己定制beam IO
  19. Axure之全局变量
  20. 减少网站跳转时间,增强网站数据安全——HSTS 详解

热门文章

  1. React 顶层 API
  2. hihocoder#1046: K个串
  3. VSCode 如何操作用户自定义代码片段
  4. Python -- 正则表达式 regular expression
  5. timeout/timelimit
  6. C# 全角半角字符互转
  7. SpringCloud基本模块分配搭建以及负载均衡
  8. 如何进行seo优化要点总结
  9. ex2
  10. AtCoder Beginner Contest 137 F