A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈永波 初级黑马   /  2012-6-30 22:22  /  1530 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈永波 于 2012-7-1 22:04 编辑

import java.util.List;

public class MyArraylistDemo {
        private Object[] arr = new Object[10];
        private int size;
        
        public  void add(Object obj){
                if(size==arr.length){
                        Object[] newArr = new Object[arr.length*3/2 +1];
                        System.arraycopy(arr, 0, newArr, 0, size);
                        arr = newArr;
                }
                arr[size++] = obj;        
        }
        public void add(int index, Object obj){
                if(size==arr.length){
                        Object[] newArr = new Object[arr.length*3/2 +1];
                        System.arraycopy(arr, 0, newArr, 0, size);
                        arr = newArr;
                }
               
                System.arraycopy(arr, index, arr, index + 1, size-index);
                arr[index] = obj;
                size++;
        }
        public void remove(int index){
               
        }
        public void remove(Object obj){
               
        }
        public void set(int index, Object obj){
                if(index >= size){
                        throw new ArrayIndexOutOfBoundsException("Index" + index + ", size" + size);
                        }
                arr[index] = obj;
        }
        public Object get(int index){
                if(index >= size){
                throw new ArrayIndexOutOfBoundsException("Index" + index + ", size" + size);
                }
                return arr[index];
        }
        public void addAll(List list){
                for(int i = 0; i < list.size(); i++){
                        add(list.get(i));
                        
                        if(size==arr.length){
                                Object[] newArr = new Object[arr.length*3/2 +1];
                                System.arraycopy(arr, 0, newArr, 0, size);
                                arr = newArr;
                        }
                }
        }
        public int size(){
                return size;
        }
        public void clear(){
               
        }

}

求        public void remove(int index){}
        public void remove(Object obj){}健壮代码   自己没什么思路了  

点评

代码看楼下的,都可以,但你要注意一点,在remove方法里要记得维护或者不维护迭代器,这会有很大差别,有些集合实现是不赞成迭代时操作集合的  发表于 2012-7-1 15:50

2 个回复

倒序浏览
楼主先给你提供一个思路吧,remove方法的返回值肯定是一个object类型的,返回的东西就是你去掉的那个元素。
所以基于这个需求,在看你的两个方法:
public void remove(int index){}
首先第一个方法要求传一个int值进来,也就是你要取走的值是第几个。你在代码中实现可以这样,首先你的要把原来的元素中的第index元素找出来,
然后新建一个新的object数组存放没有这个元素的剩下的元素 具体看代码,其实就是个错位:
public Object remove(int index)
        {
                Object o = arr[index];
                Object[] temp = arr;
                arr = new Object[arr.length-1];
                for(int i = 0 ; i < arr.length ; i ++)
                {
                        if(i>=index)
                        {
                                arr[i] = temp[i+1];
                        }
                        else
                        {
                                arr[i] = temp[i];
                        }
                }
               
                return o;
        }
其实第二个方法也一样的道理只不过实现起来代码不一样
public void remove(Object obj){}
第二个方法你就想通过一个对象然后查找到和这个对象相等的那个对象时把他返回并干掉就ok了
具体第二个代码我就不给楼主写了 不过提示一点楼主在对比对象的时候用==记得

评分

参与人数 1技术分 +1 收起 理由
刘蕴学 + 1

查看全部评分

回复 使用道具 举报
  1. public void remove(int index){

  2.         if (index>=size)
  3.         {
  4.                 // 数组越界呢
  5.         }else if (index = size -1)
  6.         {
  7.                 arr[index]=null;
  8.                 size -- ;

  9.         }else{

  10.                 System.arraycopy(arr,index+1,arr,index,size-index-1);
  11.                 arr[--size] = null ;
  12.         }
  13.                  
  14. }


  15. public void remove(Object obj){
  16.         int index = -1;
  17.         for(int i=0;i<size;i++){
  18.                 if(oby==arr[i])
  19.                 {
  20.                         index = i;
  21.                         break;
  22.                 }
  23.         }

  24.         if(index = -1){
  25.           // 找不着obj,没法移除呢
  26.         }else{
  27.                 remove(index);
  28.         }
  29.                  
  30. }
复制代码
remove(Object obj)
在remove obj时,其实它的思想是 先找到obj在这个数组中的位置,
找到位置后,给remove(int index)处理得了。

所以本质上,只要思考如何去实际
remove(int index) 这个方法就好了。

考虑这个方法是,要考虑到传入的index是否合理
为了方便处理 将 若移动是最后一位 元素就 简单了,直接置空 size-- 就可以了。

剩下的,用System.arraycopy 来搞定就可以了。

评分

参与人数 1技术分 +1 收起 理由
刘蕴学 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马