大家多多点评
package com.unti.list;
/**
*
* @author dell 2011-5-27 lihui
*
* @param <E>
*/
public class MyArray<E> {
/**
* ArrayList底层是用数组实现的,添加和删除速度慢,以为都要牵扯到内存的移动
* 按一定的排序机制
*/
private Object[] obj;
private int nodeindex = -1;// 指针 public MyArray(int size) {
obj = new Object[Math.abs(size)];
} public MyArray() {
// TODO Auto-generated constructor stub
this(10);
} public void add(E object) {
isZeng();
++nodeindex;
obj[nodeindex] = object; } public int indexOf(E object) {
for (int i = 0; i < size(); i++) {
if (obj[i].equals(object))
return i + 1;
}
return -1;
} public void add(int index, E object) {
isZeng();
if (index <= nodeindex) {
for (int i = nodeindex; i >= (nodeindex - index); i--) {
obj[i + 1] = obj[i];
}
obj[index] = object;
} else
throw new IndexOutOfBoundsException();
++nodeindex;
} private void isZeng() { if (isEmpty()) {
Object[] ob = new Object[nodeindex * 2];
for (int i = 0; i < obj.length; i++) {
ob[i] = obj[i];
}
obj = ob;
} } public void clear() {
if (nodeindex >= 0) {
obj = null;
nodeindex = -1;
}
} public boolean remove(E object) {
for (int i = 0; i < size(); i++) {
if (obj[i].equals(object)) {
for (int j = i; j < size(); j++) {
obj[j] = obj[j + 1];
}
--nodeindex;
return true;
}
}
return false;
} public boolean remove(int index) {
if (index - 1 >= 0 && index - 1 < size()) {
E object = get(index - 1);
if (object != null) {
remove(object);
return true;
}
} else {
throw new IndexOutOfBoundsException("数组越界");
}
return false; } public boolean isEmpty() {
if (nodeindex >= obj.length - 1) {
return true;
}
return false;
} public int size() {
return nodeindex + 1;
} @SuppressWarnings("unchecked")
public E get(int size) {
if (size <= size() && size >= 0)
return (E) obj[size];
else
throw new IndexOutOfBoundsException();
} public int lastIndexOf(Object object) {
for (int i = size() - 1; i > -1; i--) {
if (obj[i].equals(object)) {
return i + 1;
}
}
return -1;
} public static void main(String[] args) {
MyArray<Object> my = new MyArray<Object>();
my.add(12);
my.add("eeeeeeeeeee");
my.add(123);
my.add("wqeqawewqwe");
my.add(12);
my.add(12);
my.add(121);
my.add(3, 777777);
my.remove((Object)121);
for (int i = 0; i < my.size(); i++) {
System.out.println(my.get(i));
}
}
}
package com.unti.list;
/**
*
* @author dell 2011-5-27 lihui
*
* @param <E>
*/
public class MyArray<E> {
/**
* ArrayList底层是用数组实现的,添加和删除速度慢,以为都要牵扯到内存的移动
* 按一定的排序机制
*/
private Object[] obj;
private int nodeindex = -1;// 指针 public MyArray(int size) {
obj = new Object[Math.abs(size)];
} public MyArray() {
// TODO Auto-generated constructor stub
this(10);
} public void add(E object) {
isZeng();
++nodeindex;
obj[nodeindex] = object; } public int indexOf(E object) {
for (int i = 0; i < size(); i++) {
if (obj[i].equals(object))
return i + 1;
}
return -1;
} public void add(int index, E object) {
isZeng();
if (index <= nodeindex) {
for (int i = nodeindex; i >= (nodeindex - index); i--) {
obj[i + 1] = obj[i];
}
obj[index] = object;
} else
throw new IndexOutOfBoundsException();
++nodeindex;
} private void isZeng() { if (isEmpty()) {
Object[] ob = new Object[nodeindex * 2];
for (int i = 0; i < obj.length; i++) {
ob[i] = obj[i];
}
obj = ob;
} } public void clear() {
if (nodeindex >= 0) {
obj = null;
nodeindex = -1;
}
} public boolean remove(E object) {
for (int i = 0; i < size(); i++) {
if (obj[i].equals(object)) {
for (int j = i; j < size(); j++) {
obj[j] = obj[j + 1];
}
--nodeindex;
return true;
}
}
return false;
} public boolean remove(int index) {
if (index - 1 >= 0 && index - 1 < size()) {
E object = get(index - 1);
if (object != null) {
remove(object);
return true;
}
} else {
throw new IndexOutOfBoundsException("数组越界");
}
return false; } public boolean isEmpty() {
if (nodeindex >= obj.length - 1) {
return true;
}
return false;
} public int size() {
return nodeindex + 1;
} @SuppressWarnings("unchecked")
public E get(int size) {
if (size <= size() && size >= 0)
return (E) obj[size];
else
throw new IndexOutOfBoundsException();
} public int lastIndexOf(Object object) {
for (int i = size() - 1; i > -1; i--) {
if (obj[i].equals(object)) {
return i + 1;
}
}
return -1;
} public static void main(String[] args) {
MyArray<Object> my = new MyArray<Object>();
my.add(12);
my.add("eeeeeeeeeee");
my.add(123);
my.add("wqeqawewqwe");
my.add(12);
my.add(12);
my.add(121);
my.add(3, 777777);
my.remove((Object)121);
for (int i = 0; i < my.size(); i++) {
System.out.println(my.get(i));
}
}
}
解决方案 »
- 如何在开发过程中,减少和降低bug的出现
- 有关SPRING+CXF的问题。。跪求。。
- 简单的struts-congid.xml新手问题
- 请教有关主页大量不同模块信息的显示问题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 高分求Struts+Hibernate+Spring项目
- hibernate进行批量删除和修改
- 有关javaEE做实时监控
- QQ群6831141,欢迎热爱J2EE的朋友加入交流
- 给我个花钱用(WebLogic,WebSpere)而不用免费J2EE(Sun)的理由
- 非常奇怪的关于EJB的环境配置问题.请教高手!!
- 下面sql如何改为hql,谢谢!
- 两个javaee系统之间对接怎么做,用什么技术?
而且发现一个问题,clear之后obj是null,这时如果调用其他方法比如isEmpty之类的,估计要出异常吧
是的 先加入一个再clear,isEmpty 会报空指针。
obj = new Object[Math.abs(size)];
}2:private int nodeindex = -1;// 指针这个是不是可以改成 private int size = 0; 呢?
阅读一下优秀源码,也是种能力的提升,楼主加油!!!