java数据结构-集合
来源:互联网 发布:张逗张花 知乎 编辑:程序博客网 时间:2024/05/19 04:54
java的数据结构分为逻辑结构和物理结构(存储结构)
1. 逻辑结构 : 集合结构 ,线性结构,树形结构, 图形结构 2. 物理结构:顺序存储结构,链式存储结构
Java中的ArrayList的存储结构为顺序存储的线性结构,我模仿其源码,简单的实现MyArrayList来加深理解。
代码如下:
public class MyArrayList<E> { Object[] array;// 数组 int size;// 数组中真实存在的元素个数 private static final int DEFAULT_ARRAY_SIZE = 12; public MyArrayList() { array = new Object[0]; } public MyArrayList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException(); } array = new Object[capacity]; } public MyArrayList(Collection<? extends E> collection){ Object[] a = collection.toArray(); if (a.getClass() != Object[].class) { Object[] newArray = new Object[a.length]; System.arraycopy(a, 0, newArray, 0, a.length); a = newArray; } array = a; size = a.length; } // 扩容 private static int newCapacity(int currentCapacity) { int increntment = (currentCapacity < DEFAULT_ARRAY_SIZE / 2) ? DEFAULT_ARRAY_SIZE : currentCapacity >> 1; return currentCapacity + increntment; } // 增加 public boolean add(E e) { Object[] a = array; int s = size; if (s == a.length) { Object[] newArray = new Object[newCapacity(a.length)]; System.arraycopy(a, 0, newArray, 0, s); array = a = newArray; } a[s] = e; size++; return true; } // 删除下表元素 public E remove(int index) { Object[] a = array; int s = size; if (index > s) { throw new IndexOutOfBoundsException(); } E e = (E) a[index]; System.arraycopy(a, index + 1, a, index, --s - index); a[s] = null; size = s; return e; } // 删除元素 public boolean remove(Object object) { Object[] a = array; int s = size; if (object == null) { for (int i = 0; i < s; i++) { if (a[i] == null) { remove(i); return true; } } } else { for (int i = 0; i < s; i++) { if (object.equals(a[i])) { remove(i); return true; } } } return false; } // 修改 public E set(int index, E e) { Object[] a = array; int s = size; if (index >= s) { throw new IndexOutOfBoundsException(); } E oldE = (E) a[index]; a[index] = e; return oldE; } // 查找首次出现的位置 public int indexOf(Object object) { Object[] a = array; int s = size; if (object != null) { for (int i = 0; i < s; i++) { if (object.equals(a[i])) { return i; } } } else { for (int i = 0; i < s; i++) { if (a[i] == null) { return i; } } } return -1; } // 查找最后出现的位置 public int lastIndexOf(Object object) { Object[] a = array; int s = size; if (object != null) { for (int i = s - 1; i >= 0; i--) { if (object.equals(a[i])) { return i; } } } else { for (int i = s - 1; i >= 0; i--) { if (a[i] == null) { return i; } } } return -1; } // 获取 public E get(int index) { Object[] a = array; if (index >= size) { throw new IndexOutOfBoundsException(); } E e = (E) a[index]; return e; } // 是否为空 public boolean isEmpty() { return size == 0; } // 元素的个数 public int size() { return size; }}
测试结果如下:
public static void main(String[] args) { MyArrayList<String> myArrayList = new MyArrayList<String>(); myArrayList.add("a"); myArrayList.add("b"); myArrayList.add("c"); System.out.println(myArrayList.size()); System.out.println("-------------添加后的list----"); for (int i = 0; i < myArrayList.size(); i++) { System.out.println(myArrayList.get(i)); } myArrayList.set(2, "d"); System.out.println("-------------修改后的list---"); for (int i = 0; i < myArrayList.size(); i++) { System.out.println(myArrayList.get(i)); } myArrayList.remove(0); System.out.println("-------------删除后的list------"); for (int i = 0; i < myArrayList.size(); i++) { System.out.println(myArrayList.get(i)); } }
结果如下:
3
————-添加后的list————
a
b
c
————-修改后的list————
a
b
d
————-删除后的list————
b
d
以上是自己对ArrayList的简单的模仿,如果不对的地方,欢迎指正。
现在的Android工作不好找啊,所以有换工作的念头,也不敢轻易辞掉现在的工作。只能一步一步的提高自己。生活不易,且行且珍惜。
阅读全文
0 0
- Java集合类数据结构
- JAVA集合数据结构
- JAVA集合数据结构
- Java集合的数据结构
- Java集合与数据结构
- java 集合的数据结构
- JAVA集合数据结构
- Java集合数据结构
- [数据结构JAVA版]集合
- Java集合与数据结构
- JAVA数据结构之集合
- java集合数据结构实现
- JAVA 常用集合 数据结构
- JAVA (集合和数据结构)
- java数据结构-集合
- java的集合与数据结构
- java集合框架中的数据结构
- java集合框架中的数据结构
- 词向量源码解析:(5.8)ngram2vec源码解析之counts2ppmi等
- robotframework ‘ascii’ codec can’t decode byte 0xe6 in position 977
- 内存地址空间布局
- FFmpeg的IO分析
- Storm的相关概念
- java数据结构-集合
- 背包问题
- uva 1374 Power Calculus
- TensorFlow架构系统理解
- HDU 2054
- Single Number
- 《Volley源码分析》Part1、Volley的使用以及介绍
- .NET获取聚合数据接口数据(Http协议POST和GET请求)
- ssh免密登录实现办法及原理