数组第二十四课,模拟ArrayList容器的底层实现,JDK源码分析
来源:互联网 发布:nba2013 14总决赛数据 编辑:程序博客网 时间:2024/05/02 01:02
package com.pkushutong.MyCollection;/** * 模拟实现JDK中提供的ArrayList类 * @author dell * */public class MyArrayList {/** * The value is used for Objects storage. */ private Object[] value; /** * The size is the number of Objects used. */ private int size; public MyArrayList(){ //value = new Object[16]; this(10); } //重载构造方法 public MyArrayList(int size){ if(size < 0){ try {throw new Exception();} catch (Exception e) {e.printStackTrace();} } value = new Object[size]; } //返回添加方法里的条目数量 public int size(){ return size; } //返回条目数量是否为空 public boolean isEmpty() { return size == 0; } public int indexOf(Object obj){ if(obj == null){ return -1; }else{ for(int i = 0; i < value.length; i++){ if(obj == value[i]){ return i; } } }return -1; } /** * 给容器里加东西 */ public void add(Object obj){ value[size] = obj; size++; if(size >= value.length){ //装不下了,扩容吧 int newCapacity = value.length * 2; Object[] newList = new Object[newCapacity]; for(int i = 0; i < value.length; i++){ newList[i] = value[i]; } value = newList; } } public Object get(int index){ if(index < 0 || index > size - 1){//0到size-1之间 try {throw new Exception();} catch (Exception e) {e.printStackTrace();} } return value[index]; } public int lastIndexOf(Object obj) { if (obj == null) { return -1; } else { for (int i = value.length-1; i >= 0; i--) if (obj==value[i]) return i; } return -1; } public Object set(int index, Object obj) { if(size < 0){ try {throw new Exception();} catch (Exception e) {e.printStackTrace();} } Object old = value[index]; value[index] = obj; return old; } public static void main(String[] args) {MyArrayList list = new MyArrayList(2);list.add("abcde");list.add(new Human("樊")); Human h = (Human) list.get(1);System.out.println(h.getName());//System.out.println(list.get(0));System.out.println(list.size());}}
0 0
- 数组第二十四课,模拟ArrayList容器的底层实现,JDK源码分析
- 59_数组_模拟ArrayList容器的底层实现_JDK源码分析ArrayList
- java学习之旅59--模拟ArrayList容器的底层实现_JDK源码分析ArrayList
- 容器第三课,JDK源码分析,自己实现ArrayList数组扩容
- 模拟ArrayList底层实现
- 第二人生的源码分析(十四)人物角色的实现
- jdk源码分析--ArrayList
- ArrayList底层实现源码解析
- ArrayList底层实现源码解读
- JDK源码分析-ArrayList分析
- JDK中ArrayList的实现分析
- 蔡军生先生第二人生的源码分析(十四)人物角色的实现
- jdk源码分析之ArrayList
- JDK 1.8 ArrayList源码分析
- JDK源码解析---ArrayList分析
- 集合第一步:模拟ArrayList底层实现
- 自己编码模拟实现ArrayList底层代码
- [Jdk源码阅读]ArrayList实现
- iOS学习笔记(一)——ios搭建开发环境
- 【小常识】Appdata\Local Roaming LocalLow文件夹简介
- poj 1182 食物链 带权并查集
- Android Studio 初体验
- [转载]eclipse中svn的各种图标详解 问号
- 数组第二十四课,模拟ArrayList容器的底层实现,JDK源码分析
- C++ Primer 第八章 标准IO库
- 让你的网站支持手机二维码登录
- 数据结构
- 日期和数组
- MFC 对话框Border属性为none时通过鼠标自由拉伸窗口大小
- boost -- regex 学习笔记
- android应用安全——数据安全
- 项目团队人员的有效沟通