用Java实现ArrayList类删除方法

用Java实现ArrayList类删除方法,java,arraylist,Java,Arraylist,我必须实现一个定制的ArrayList类。我们不能使用arrayCopy。我需要能够从数组中删除一个字符串,然后将所有元素向左移动一个索引。我的尝试在下面,请帮忙 /**************************************************************************** * Removes the string at the specified index from the list, * if it is present and shifts

我必须实现一个定制的ArrayList类。我们不能使用arrayCopy。我需要能够从数组中删除一个字符串,然后将所有元素向左移动一个索引。我的尝试在下面,请帮忙

/****************************************************************************
 * Removes the string at the specified index from the list,
 * if it is present and shifts the remaining elements left.
 *
 * @param  str value to remove from list
 * @return the value removed from the list
 * @throws IndexOutOfBoundsException if index is invalid
 */
    public String remove(int index){
        if (index < 0 || index >= this.myArray.length)
        {
            throw new IndexOutOfBoundsException("Index out of bounds.");
        }
        else {
        String removed = this.myArray[index];
        this.myArray[index] = null;
        String [] temp = new String[this.myArray.length-1];
        for(int i = 0; i<this.myArray.length; i++){
        if (this.myArray[i] != null){
            temp[i] = this.myArray[i];
        }
    }
        return removed;
    }
    }       
/****************************************************************************
*从列表中删除指定索引处的字符串,
*如果存在,则将剩余元素向左移动。
*
*@param str要从列表中删除的值
*@返回从列表中删除的值
*@如果索引无效,则引发IndexOutOfBoundsException
*/
删除公共字符串(整型索引){
if(index<0 | | index>=this.myArray.length)
{
抛出新的IndexOutOfBoundsException(“索引超出界限”);
}
否则{
String removed=this.myArray[index];
this.myArray[index]=null;
String[]temp=新字符串[this.myArray.length-1];

对于(int i=0;i您正在创建一个
temp
数组,其元素数比
this.myArray
少一个。然后您迭代
myArray
的所有索引,并使用这些索引写入
temp[i]
。最后一个是超出范围的,因为
temp
比这个小一个


调试器将帮助您找到这一点。您还可以在访问数组的任何行之前放置一个
System.out.println(“即将访问索引”+i)
,然后查看在异常之前打印哪一行。然后您只需要确定要访问哪一个索引(它就在stdout中)想想你将要访问的数组有多大。

你正在创建一个
temp
数组,其元素比
this.myArray
少一个。然后你迭代
myArray
的所有索引,并使用这些索引写入
temp[i]
。最后一个是不允许的,因为
temp
要小一个


调试器将帮助您找到这一点。您还可以在访问数组的任何行之前放置一个
System.out.println(“即将访问索引”+i)
,然后查看在异常之前打印哪一行。然后您只需要确定要访问哪一个索引(它就在stdout中)想想你将要访问的数组有多大。

你正在创建一个
temp
数组,其元素比
this.myArray
少一个。然后你迭代
myArray
的所有索引,并使用这些索引写入
temp[i]
。最后一个是不允许的,因为
temp
要小一个


调试器将帮助您找到这一点。您还可以在访问数组的任何行之前放置一个
System.out.println(“即将访问索引”+i)
,然后查看在异常之前打印哪一行。然后您只需要确定要访问哪一个索引(它就在stdout中)想想你将要访问的数组有多大。

你正在创建一个
temp
数组,其元素比
this.myArray
少一个。然后你迭代
myArray
的所有索引,并使用这些索引写入
temp[i]
。最后一个是不允许的,因为
temp
要小一个


调试器将帮助您找到这一点。您还可以在访问数组的任何行之前放置一个
System.out.println(“即将访问索引”+i)
,然后查看在异常之前打印哪一行。然后您只需要确定要访问哪一个索引(它就在stdout中)想想你将要访问的数组有多大。

temp
数组要短一个,所以它不能容纳所有的东西

复制数组时,需要跳过所需的索引

下面的代码通过对新旧数组中的索引使用两个不同的变量来实现这一点

当遇到删除的索引时,它跳过递增其中一个索引

public String remove(int index) {
    if (index < 0 || index >= this.myArray.length) {
        // FYI, this would be thrown anyway; not sure if you need to do it
        throw new IndexOutOfBoundsException("Index out of bounds.");
    }
    String removed = this.myArray[index];
    String[] temp = new String[this.myArray.length - 1];
    for(int i = 0, j = 0; i < this.myArray.length; i++){
        if (i != index) {
            temp[j++] = this.myArray[i];
        }
        // otherwise, j does not get incremented
    }
    this.myArray = temp; // don't forget this!
    return removed;
}
删除公共字符串(int索引){
if(index<0 | | index>=this.myArray.length){
//仅供参考,这将被抛出无论如何,不确定你是否需要这样做
抛出新的IndexOutOfBoundsException(“索引超出界限”);
}
String removed=this.myArray[index];
String[]temp=新字符串[this.myArray.length-1];
for(int i=0,j=0;i
临时
temp
数组短了一个,所以它不能容纳所有的内容

复制数组时,需要跳过所需的索引

下面的代码通过对新旧数组中的索引使用两个不同的变量来实现这一点

当遇到删除的索引时,它跳过递增其中一个索引

public String remove(int index) {
    if (index < 0 || index >= this.myArray.length) {
        // FYI, this would be thrown anyway; not sure if you need to do it
        throw new IndexOutOfBoundsException("Index out of bounds.");
    }
    String removed = this.myArray[index];
    String[] temp = new String[this.myArray.length - 1];
    for(int i = 0, j = 0; i < this.myArray.length; i++){
        if (i != index) {
            temp[j++] = this.myArray[i];
        }
        // otherwise, j does not get incremented
    }
    this.myArray = temp; // don't forget this!
    return removed;
}
删除公共字符串(int索引){
if(index<0 | | index>=this.myArray.length){
//仅供参考,这将被抛出无论如何,不确定你是否需要这样做
抛出新的IndexOutOfBoundsException(“索引超出界限”);
}
String removed=this.myArray[index];
String[]temp=新字符串[this.myArray.length-1];
for(int i=0,j=0;i
临时
temp
数组短了一个,所以它不能容纳所有的内容

复制数组时,需要跳过所需的索引

下面的代码通过对新旧数组中的索引使用两个不同的变量来实现这一点

当遇到删除的索引时,它跳过递增其中一个索引

public String remove(int index) {
    if (index < 0 || index >= this.myArray.length) {
        // FYI, this would be thrown anyway; not sure if you need to do it
        throw new IndexOutOfBoundsException("Index out of bounds.");
    }
    String removed = this.myArray[index];
    String[] temp = new String[this.myArray.length - 1];
    for(int i = 0, j = 0; i < this.myArray.length; i++){
        if (i != index) {
            temp[j++] = this.myArray[i];
        }
        // otherwise, j does not get incremented
    }
    this.myArray = temp; // don't forget this!
    return removed;
}
删除公共字符串(int索引){
if(ind