自定义容器
自定义容器能更好的帮助理解集合的概念
1) 利用数组构建
容器采用顺序存储
package com.lihao.javapractice.day01;
import java.util.Arrays;
/**
* 通过数组创建一个自定义的容器
*
* @author Lenovo
*
* 2020年5月16日
*/
public class MyContainer {
private Object[] data;
private int total;
public MyContainer() {
data = new Object[10];
}
// 添加容器内部成员
public void add(Object obj) {
if (total >= data.length) {
// Arrays.copyof用于数组扩容
data = Arrays.copyOf(data, data.length * 2);
}
data[total++] = obj;
}
// 查看容器内部成员
public Object[] toArray() {
return Arrays.copyOf(data, total);
}
// 根据索引获取所在的obj
public Object getObj(int index) {
if (index < 0 || index >= total) {
throw new IndexOutOfBoundsException();
}
return data[index];
}
// 根据obj获取索引值,没有返回-1
public int getIndex(Object obj) {
int index = -1;
if (obj == null) {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
} else {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
}
return index;
}
// 删除容器内部成员
public void delate(Object obj) {
int i = getIndex(obj);
if(i!=-1){
System.arraycopy(data, i+1, data, i, total-1-i);
data[--total]=null;
}
}
}
测试
package com.lihao.javapractice.day01;
import java.util.Arrays;
/**
* 通过数组创建一个自定义的容器
*
* @author Lenovo
*
* 2020年5月16日
*/
public class MyContainer {
private Object[] data;
private int total;
public MyContainer() {
data = new Object[10];
}
// 添加容器内部成员
public void add(Object obj) {
if (total >= data.length) {
// Arrays.copyof用于数组扩容
data = Arrays.copyOf(data, data.length * 2);
}
data[total++] = obj;
}
// 查看容器内部成员
public Object[] toArray() {
return Arrays.copyOf(data, total);
}
// 根据索引获取所在的obj
public Object getObj(int index) {
if (index < 0 || index >= total) {
throw new IndexOutOfBoundsException();
}
return data[index];
}
// 根据obj获取索引值,没有返回-1
public int getIndex(Object obj) {
int index = -1;
if (obj == null) {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
} else {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
}
return index;
}
// 删除容器内部成员
public void delate(Object obj) {
int i = getIndex(obj);
if(i!=-1){
System.arraycopy(data, i+1, data, i, total-1-i);
data[--total]=null;
}
}
}
2) 利用Node类
单项链式存储
每个存储单元包括data和指向下一个存储单元的地址
package com.lihao.javapractice.day01;
import java.util.Arrays;
/**
* 通过数组创建一个自定义的容器
*
* @author Lenovo
*
* 2020年5月16日
*/
public class MyContainer {
private Object[] data;
private int total;
public MyContainer() {
data = new Object[10];
}
// 添加容器内部成员
public void add(Object obj) {
if (total >= data.length) {
// Arrays.copyof用于数组扩容
data = Arrays.copyOf(data, data.length * 2);
}
data[total++] = obj;
}
// 查看容器内部成员
public Object[] toArray() {
return Arrays.copyOf(data, total);
}
// 根据索引获取所在的obj
public Object getObj(int index) {
if (index < 0 || index >= total) {
throw new IndexOutOfBoundsException();
}
return data[index];
}
// 根据obj获取索引值,没有返回-1
public int getIndex(Object obj) {
int index = -1;
if (obj == null) {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
} else {
for (int i = 0; i < total; i++) {
if (obj.equals(data[i])) {
index = i;
break;
}
}
}
return index;
}
// 删除容器内部成员
public void delate(Object obj) {
int i = getIndex(obj);
if(i!=-1){
System.arraycopy(data, i+1, data, i, total-1-i);
data[--total]=null;
}
}
}
测试代码
package com.lihao.javapractice.day01;
public class TestMyConCopy {
public static void main(String[] args) {
MyConCopy my =new MyConCopy();
// 添加对象
my.add("张三");
my.add("李四");
my.add("王超");
my.add("马汉");
my.add("张龙");
my.add("赵虎");
// 删除对象
my.delate("马汉");
Object[] myarry=my.toArray();
for(int i=0;i<myarry.length;i++){
System.out.println(myarry[i]);
}
}
}