手工实现Array List和Linked List
Array List样例:
/**
* 增加泛型
* 自动增加数组容量
* 增加set、get方法;增加数组边界的检查
* 增加remove方法
*/
package cn.study.lu.four;
public class MyArrayList <E>{
private Object[] elementData;
private int size;
private static final int DEFALT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFALT_CAPACITY];
}
public MyArrayList(int capacity) {
elementData = new Object[capacity];
}
public void add(E e) {
if (size==elementData.length) {
Object[] newArray = new Object[elementData.length+(elementData.length>>1)];
System.arraycopy(elementData, 0, newArray, 0, elementData.length);
elementData = newArray;
}
elementData[size++] = e;
}
public Object get(int index) {
Cheak(index);
return (E)elementData[index];
}
public void set(E e,int index) {
Cheak(index);
elementData[index] = e;
}
public void Cheak(int index) {
if (index<0 ||index>size ) {
throw new RuntimeException("索引不合法"+index);
}
}
public void remove(E e) {
//将e和所有元素挨个比较,获得第一个为true的,返回
for (int i = 0; i < size; i++) {
if (e.equals(get(i))) {
remove(i);
}
}
}
public void remove(int index) {
int a = elementData.length-index-1;
if (a>0) {
System.arraycopy(elementData, index+1, elementData, index,a );
}
elementData[--size] = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
return size==0?true:false;
}
public String toString() {
StringBuilder str = new StringBuilder();
str.append("[");
for (int i = 0; i <size; i++) {
str.append(elementData[i]+",");
}
str.setCharAt(str.length()-1, ']');
return str.toString();
}
public static void main(String[] args) {
MyArrayList s1 = new MyArrayList(20);
// s1.add("aa");
// s1.add("bb");
// System.out.println(s1);
for (int i = 0; i < 40; i++) {
s1.add("aaa"+i);
}
System.out.println(s1);
s1.set("啦啦啦", 39);
System.out.println(s1.get(39));
s1.remove(2);
System.out.println(s1);
System.out.println(s1.size());
System.out.println(s1.isEmpty());
}
}
Linked List样例:
package cn.study.lu.four;
/**
* 自定义一个链表并实现打印
*增加泛型
* 增加get方法
* 增加remove的两个方法
* 增加set方法
*
*/
public class MyLinkedList<E> {
private Node first;
private Node last;
private int size;
public void add(E e) {
Node node = new Node(e);
if(first == null) {
first = node;
last = node;
}else {
node.previous=last;
node.next = null;
last.next = node;
last = node;
}
size++;
}
public String toString() {
StringBuilder str = new StringBuilder("[");
Node temp = first;
while (temp!=null) {
str.append(temp.element+",");
temp = temp.next;
}
str.setCharAt(str.length()-1, ']');
return str.toString();
}
public E get(int index) {
Node temp = null;
if (index<0||index>size-1) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size-1)/2)) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
}else {
temp = last;
for (int i = size-1; i > index; i--) {
temp = temp.previous;
}
}
return (E)temp.element;
}
public void remove(E e) {
Node temp = first;
while (temp.element != e) {
temp = temp.next;
}
if (temp==null) {
throw new RuntimeException("不存在这个对象!");
}else {
temp.previous.next = temp.next;
temp.next.previous= temp.previous;
size--;
}
}
public void remove(int index) {
Node temp = null;
if (index<0||index>size-1) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size-1)/2)) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
}else {
temp = last;
for (int i = size-1; i >index; i--) {
temp = temp.previous;
}
}
if (index==0) {
temp.next.previous = null;
first = temp.next;
}else if (index == (size-1)) {
temp.previous.next = null;
last = temp.previous;
} else {
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
}
size--;
}
public void set(int index,E e) {
Node a = new Node(e);
Node temp = null;
if (index<0||index>size) {
throw new RuntimeException("索引不合法:"+index);
}
if (index<=((size)/2)) {
temp = first;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
}else if(index!=size) {
temp = last;
for (int i = size-1; i >index; i--) {
temp = temp.previous;
}
}else {
temp=last;
}
if (index == 0) {
a.next = temp;
temp.previous=a;
first = a;
}else if (index==size) {
a.previous=temp;
temp.next = a;
last =a;
}else {
a.previous=temp.previous;
temp.previous.next = a;
a.next = temp;
temp.previous = a;
}
size++;
}
public static void main(String[] args) {
MyLinkedList<String> a = new MyLinkedList<>();
a.add("aaa");
a.add("bbb");
a.add("ccc");
a.add("ddd");
a.add("eee");
a.add("fff");
a.add("ggg");
System.out.println(a);
System.out.println(a.get(5));
a.remove(0);
System.out.println(a);
System.out.println(a.get(5));
a.set(0, "aaa");
System.out.println(a);
System.out.println(a.getSize());
System.out.println(a.get(5));
}
}
class Node{
Node previous;
Node next;
Object element;
public Node(Node previous, Node next, Object element) {
super();
this.previous = previous;
this.next = next;
this.element = element;
}
public Node(Object element) {
super();
this.element = element;
}
}
手工实现Array List和Linked List的更多相关文章
- Lintcode489-Convert Array List to Linked List-Easy
489. Convert Array List to Linked List Convert an array list to a linked list. Example Example 1: In ...
- Array List和Linked List实现分析
一,前言 先来一张Collection集合图. 今天分享一些关于Collection集合中的List,讲真的集合这东西在网上真是老生常谈了.说实话连本人都觉得腻了(哈哈),但是话又说回来,整个 ...
- Leetcode: sliding window maximum
August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...
- Java基础知识之集合(容器)简介
JAVA的集合体系,主要由Collection接口,Map接口,Iterator接口和操作集合的工具类Collections组成.其中的Iterator只是一个迭代器,真正的容器则派生自Collect ...
- 【转】Java集合框架综述
文章目录 1. 集合框架(collections framework) 2. 设计理念 3. 两大基类Collection与Map 3.1. Collection 3.2. Map 4. 集合的实现( ...
- 设计模式- 主动对象(Active Object)
译者注:1.对象分为主动对象和被动对象,主动对象内部包含一个线程,可以自动完成动作或改变状态,而一般的被动对象只能通过被其他对象调用才有所作为.在多线程程序中,经常把一个线程封装到主动对象里面.2.在 ...
- The C5 Generic Collection Library for C# and CLI
The C5 Generic Collection Library for C# and CLI https://github.com/sestoft/C5/ The C5 Generic Colle ...
- Java基础之集合:概览
Java Basic->Collections->Overview 先抛一个问题,用一个类似树形的结构,介绍下 Java 的集合类数据结构:有哪些,从简单到复杂,有怎么样的继承关系. 下面 ...
- 【转】图片缓存之内存缓存技术LruCache、软引用 比较
每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...
随机推荐
- 高通平台Camera调试(一)【转】
本文转载自:http://www.voidcn.com/blog/Winva/article/p-6044730.html 4.3. Camera 参考文档: 1) 80-NA157-22_PRESE ...
- malloc(50) 内存泄露 内存溢出 memory leak会最终会导致out of memory
https://en.wikipedia.org/wiki/Memory_leak In computer science, a memory leak is a type of resource l ...
- easyui表格适应bootstrap
.panel1 { overflow: hidden; text-align: left; margin:; border:; -moz-border-radius: 0 0 0 0; -webkit ...
- VMware 虚拟化编程(15) — VMware 虚拟机的恢复方案设计
目录 目录 前文列表 将已存在的虚拟机恢复到指定时间点 恢复为新建虚拟机 灾难恢复 恢复细节 恢复增量备份数据 以 RDM 的方式创建虚拟磁盘 创建虚拟机 Sample of VirtualMachi ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_6_练习_转换文件编码
- 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第4节 System类_3_System类的常用方法
复制数组
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_05 List集合_1_List集合_介绍&常用方法
有序的,还包含索引,允许有重复的值 . add 打印出来的不是地址,说明重写了toString的方法 remove方法 返回的是被移除的元素 set方法 get 索引越界异常 几种越界的异常
- PHP 图片+文字+二维码生成小程序分享海报
思路: 1.请求微信接口获取一定尺寸微信二维码 2.准备海报主图,处理尺寸按比例缩放 3.准备分享语录,计算段落高度 4.生成海报:创建画布,分写别入按顺序和位置写入二维码.图片.文字等 5.保存海报 ...
- 【ABAP系列】SAP GUI740 PATCH5出现弹窗BUG
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP GUI740 PATCH ...
- Java多态性应用——多态数组、多态参数
多态数组: Person[] person = {new Person("张三", 32), new Student("李四", 21, 120, 90.0), ...