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的更多相关文章

  1. 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 ...

  2. Array List和Linked List实现分析

    一,前言 ​ 先来一张Collection集合图. ​ 今天分享一些关于Collection集合中的List,讲真的集合这东西在网上真是老生常谈了.说实话连本人都觉得腻了(哈哈),但是话又说回来,整个 ...

  3. Leetcode: sliding window maximum

    August 7, 2015 周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# Sorte ...

  4. Java基础知识之集合(容器)简介

    JAVA的集合体系,主要由Collection接口,Map接口,Iterator接口和操作集合的工具类Collections组成.其中的Iterator只是一个迭代器,真正的容器则派生自Collect ...

  5. 【转】Java集合框架综述

    文章目录 1. 集合框架(collections framework) 2. 设计理念 3. 两大基类Collection与Map 3.1. Collection 3.2. Map 4. 集合的实现( ...

  6. 设计模式- 主动对象(Active Object)

    译者注:1.对象分为主动对象和被动对象,主动对象内部包含一个线程,可以自动完成动作或改变状态,而一般的被动对象只能通过被其他对象调用才有所作为.在多线程程序中,经常把一个线程封装到主动对象里面.2.在 ...

  7. 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 ...

  8. Java基础之集合:概览

    Java Basic->Collections->Overview 先抛一个问题,用一个类似树形的结构,介绍下 Java 的集合类数据结构:有哪些,从简单到复杂,有怎么样的继承关系. 下面 ...

  9. 【转】图片缓存之内存缓存技术LruCache、软引用 比较

    每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...

随机推荐

  1. SQL 多表查询的几种连接方式

    --创建数据库 create database GoodsSystem go --使用数据库 use GoodsSystem go --创建商品类型表 create table GoodsType ( ...

  2. iOS环境搭建

    Xcode安装 一定要在App Store上下载XCode . git config常用配置 设置lg命令 查看分支图 git config --global alias.lg "log - ...

  3. 2013 AAAI: Uncorrelated Lasso

    Si-Bao Chen, Chris Ding, Bin Luo and Ying Xie. Uncorrelated Lasso. AAAI, 2013. 第一作者是安徽大学陈思宝副教授. 第二作者 ...

  4. CGI FastCGI WSGI 解析

    我们将服务端程序分为了web服务器和应用程序服务器. web服务器是用于处理HTML文件,让客户可以通过浏览器进行访问.主流的有apache,IIS,nginx,lghttpd等. 应用服务器处理业务 ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第4节 模拟BS服务器案例_2_模拟BS服务器代码实现

    这三行代码是固定的在输出之前 浏览器再次访问这个页面. 图片没有显示出来 复制刚才的代码一份出来重命名 加个while循环.把代码都放进去. 然后在while里面开启一个线程.把读取的代码都放在线程里 ...

  6. 3 hadoop安装Standalone模式和伪分布式模式

    1 Standalone模式默认模式,即默认配置即是standalone模式装好hadoop-client,会将依赖的包安装好yum install hadoop-client配置文件各个配置文件在/ ...

  7. GMS认证测试FQA

    ---摘要 本文档用于收录GMS认证测试的异常问题,提供一般性指导.对于本文档中未提供解答的问题请咨询@开发经理或@领域技术专家 cts测试工具如何获取? A:见Google官网 https://so ...

  8. ubuntu彩色图形界面

    Ubuntu的默认 ~/.bashrc 文件里,有一个控制是否打开彩色提示符文件的变量 $force_color_promt,只需要打开这个变量的开关,就可以使用彩色的命令行提示符了. 这对于输查看命 ...

  9. mooc-IDEA live template--006

    十二.IntelliJ IDEA -live template 以定时器为例: 1.创建一个Template Group... 2.在创建的Template Group下面,创建一个Live Temp ...

  10. Mac入门--如何使用brew安装多个PHP版本

    一 安装7.1 1. 安装PHP7.1 brew install php@7.1 2. 修改配置 php-fpm.conf,一般在/usr/local/etc/php下(如果php-fpm.conf中 ...