【问题标题】:Implementing ArrayList add Method manually using arrays of objects [duplicate]使用对象数组手动实现 ArrayList add 方法[重复]
【发布时间】:2015-04-07 19:00:53
【问题描述】:

您好,我需要手动实现arraylist.add() 方法只使用数组和数组复制方法,但我在执行时遇到了麻烦。该方法的规范是该方法在指定位置插入一个元素,并将当前位于该位置的任何元素向右移动,并将索引加一,将数组的大小扩大一,以便所有元素都适合。有人请帮忙。

    private Object [] list;
    final int maxObjects = 100;

    public ListOfObjects()
    {
        list= new Object[maxObjects];
    }
    public ListOfObjects(Object[]o)
    {
        list= o;

    }
    public void add(Object element,int index)
    {
        Object[] newData = new Object[list.length+1];
        for(int i =0; i < index; i++)
        {
            newData[i] = list[i];
            newData[list] = element;
        }

        for(int i = index; i < list.length; i++)
        {
            newData[i+1] = list[i];
        }
    }

【问题讨论】:

标签: java arrays object for-loop arraylist


【解决方案1】:

此解决方案利用了 ArrayList 迭代器,它以正确的顺序返回对象:

    ArrayList<Object> elementInserter(ArrayList<Object> inArray, Object element, int index){
       ArrayList<Object> outArray = new ArrayList<Object>(inArray.size() + 1);
       outArray.addAll(inArray.subList(0, index));
       outArray.add(element);
       outArray.addAll(inArray.subList(index, inArray.size()));
       return outArray;
   }

【讨论】:

    【解决方案2】:

    你的逻辑在我看来是错误的。你应该做类似的事情 -

        Object[] newData = new Object[list.length+1];
        for(int i =0; i < index; i++)
        {
            newData[i] = list[i];
        }
        newData[index] = element;
        for(int i = index; i < list.length; i++)
        {
            newData[i+1] = list[i];
        }
    

    【讨论】:

    • 你知道我如何将 system.arraycopy 实现到这个中以便我可以扩大尺寸吗?
    • system.arraycopy 用于将数组从源复制到目标。您必须为索引单独执行此操作:i=0 to i=index-1,然后从索引:i=index+1 to i=newData.length-1
    • 在测试这个方法时,我总是在线程“main”中得到这个错误异常 java.lang.ArrayIndexOutOfBoundsException: 3 at newData[i+1] = list[i] at cse214hw1.CardCollection.main(CardCollection .java:139)
    • 我的错误。在第二个 for 循环中应该是 list.length
    • 我应该把array.system副本放在哪里?我目前在 forloops System.arraycopy(list, index, newData, index+1, list.length - index);
    【解决方案3】:

    System.arrayCopy 的 javadoc 专门针对 src 和 dest 是同一个数组的情况:

    如果 src 和 dest 参数引用同一个数组对象,那么 复制就像在 srcPos 位置上的组件一样执行 srcPos+length-1 首先被复制到一个临时数组中,长度为 组件,然后复制临时数组的内容 通过目标的 destPos+length-1 进入位置 destPos 数组。

    如果您的支持list 足够大,那么您只需使用arrayCopy 将受影响的索引移到1 以上。

    //shift everything after the index over
    System.arrayCopy(list, index, list, index + 1, list.length - index);
    //place new value in index
    list[index] = element;
    

    否则你需要创建一个新数组,然后使用arrayCopy复制插入索引之前的所有内容。

    Object[] newList = new Object[calcSize()];
    //first copy everything before index if index is not 0
    if (index > 0)
    {
        System.arrayCopy(list, 0, newList, 0, index);
    }
    newList[index] = element;
    System.arrayCopy(list, index, newList, index+1, list.length - index);
    

    【讨论】:

    • 我可以在我当前的代码中实现它吗?
    【解决方案4】:

    将元素添加到对象数组的索引中,

    Object[] myObjects;
    
    public static void addObject(Object obj, int index) {
    
    // Assuming you want something in your empty array
         if(myObjects == null) {
            myObjects = new Object[] { obj };
            return;
        } 
        ArrayList<Object> temp = new ArrayList<Object>();
        for(int i = 0; i < myObjects.length; i++) { 
            if(i == index)
               temp.add(obj);
            temp.add(myObjects[i]);
        }
        myObjects = temp.toArray(new Object[temp.size()]);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2015-03-10
      • 1970-01-01
      • 2021-03-17
      • 2021-12-29
      • 2011-03-06
      • 1970-01-01
      • 2020-11-12
      • 2013-11-08
      相关资源
      最近更新 更多