java学习 MySet简易集合(皮毛)

来源:互联网 发布:java软件开发硬件环境 编辑:程序博客网 时间:2024/04/29 16:55

数组对某一类数据的集合,为集合是对不同数据类型的集合

package cn.hncu.set;

public class MySet {
private Object[] objs=new Object[0];//可new空数组
private boolean flag;//是否加入的对象重复允许

public MySet(boolean flag){
this.flag=flag;
}
public MySet(){
this.flag=false;
}
/*
* 加入对象
*/
public boolean add(Object obj){
if (flag) {
if (contain(obj))
return false;
}
if(objs.length>size()){
objs[size()]=obj;
}else{
Object[] tempobj=new Object[objs.length*2+1];//以2*n+1递开
System.arraycopy(objs, 0, tempobj, 0, objs.length);
tempobj[objs.length]=obj;
objs=tempobj;
tempobj=null;
}
return true;
}
/*
* 对象加第一个
*/
public boolean addFirst(Object obj){
if (flag) {
if (contain(obj))
return false;
}
if(objs.length>size()){
System.arraycopy(objs, 0, objs, 1, size());
objs[0]=obj;
}else{
Object[] tempobj=new Object[objs.length*2+1];
System.arraycopy(objs, 0, tempobj, 1, objs.length);
tempobj[0]=obj;
objs=tempobj;
tempobj=null;
}
return true;
}
/*
* 对象加指定pos位置
*/
public boolean add(Object obj,int pos){
if (flag) {
if (contain(obj))
return false;
}
if(pos<0||pos>=objs.length)
return false;
if(objs.length>size()){
System.arraycopy(objs, pos, objs, pos+1, size()-pos);
objs[pos]=obj;
}else{
Object[] tempobj=new Object[objs.length*2+1];
System.arraycopy(objs, 0, tempobj, 0, objs.length);
System.arraycopy(tempobj, pos, tempobj, pos+1, size()-pos);
tempobj[pos]=obj;
objs=tempobj;
tempobj=null;
}
return true;
}


/*
* 移除所有对象
*/
public boolean removeAll(){
objs=new Object[0];
return true;
}
/*
* 移除first-end(首位包括)
*/
public boolean remove(int first,int end){
if(first<0||first>=objs.length)
return false;
if(end<0||end>=objs.length)
return false;
if(first>end||end-first+1>objs.length)
return false;
Object[] obj=new Object[objs.length-(end-first+1)];
System.arraycopy(objs, 0, obj, 0, first);
System.arraycopy(objs, end+1, obj, first, objs.length-end-1);
objs=obj;
obj=null;
return true;
}
/*
* 移除第first个对象
*/
public boolean remove(int first){
if(first<0||first>=objs.length)
return false;
Object[] obj=new Object[objs.length-1];
System.arraycopy(objs, 0, obj, 0, first);
System.arraycopy(objs, first+1, obj, first, objs.length-first-1);
objs=obj;
obj=null;
return true;
}
/*
* 获得所有对象
*/
    public Object[] getAll(){
    Object[] obj=new Object[size()];
    System.arraycopy(objs, 0, obj, 0, obj.length);
return obj;
}
    /*
     * 获得first-end(首位包括)
     */
public Object[] get(int first,int end){
if(first<0||first>=objs.length)
return null;
if(end<0||end>=objs.length)
return null;
if(first>end||end-first+1>objs.length)
return null;
Object[] obj=new Object[end-first+1];
System.arraycopy(objs, first, obj, 0, obj.length);
return obj; 
}
/*
* 获得第first个对象
*/
public Object get(int first){
if(first<0||first>=objs.length)
return null;
return objs[first];
}
/*
* 判断对象是否存在
*/
    public boolean contain(Object obj){
for (int i=0;i<size();i++) {
if(objs[i]==null){
continue;
}
if(objs[i].equals(obj))
return true;
}
return false;
}
/*
* 获得对象的数目
*/
    public int size(){
        for (int i = objs.length-1; i >=0 ; i--) {
if(objs[i]!=null)
return i+1;
}
return 0;
}
   
    public Object[] getObjs() {
return objs;
}
public void setObjs(Object[] objs) {
this.objs = objs;
}
}
0 0
原创粉丝点击