繁体   English   中英

从Java中的ArrayList中删除所有特定值

[英]Removing all of a certain value from an ArrayList in java

我已获得以下ArrayList的代码,并且必须在此类中编写一个新方法,该方法将从ArrayList中删除所有给定值。 我一直在尝试编写此函数,但是由于某种原因无法弄清楚如何访问列表。 我必须对LLList做同样的事情,所以我叫头节点,但是我不明白如何访问数组。

/*
 * ArrayList.java
 *
 */

import java.util.*;

/*
 * A class that implements our simple List interface using an array.
*/
public class ArrayList implements List {
private Object[] items;     // the items in the list
private int length;         // # of items in the list

/*
 * Constructs an ArrayList object with the specified maximum size
 * for a list that is initially empty.
 */
public ArrayList(int maxSize) {
    items = new Object[maxSize];
    length = 0;
}

/*
 * Constructs an ArrayList object containing the items in the specified
 * array, and with a max size that is twice the size of that array 
 * (to allow room for growth).
 */
public ArrayList(Object[] initItems) {
    items = new Object[2 * initItems.length];        
    for (int i = 0; i < initItems.length; i++) {
        items[i] = initItems[i];
    }

    length = initItems.length;
}

/*
 * length - returns the number of items in the list 
 */
public int length() {
    return length;
}

/* 
 * isFull - returns true if the list is full, and false otherwise
 */
public boolean isFull() {
    return (length == items.length);
}

/* getItem - returns the item at position i in the list */
public Object getItem(int i) {
    if (i < 0 || i >= length) {
        throw new IndexOutOfBoundsException();
    }

    return items[i];
}

/* 
 * addItem - adds the specified item at position i in the list,
 * shifting the items that are currently in positions i, i+1, i+2,
 * etc. to the right by one.  Returns false if the list is full,
 * and true otherwise.
 */
public boolean addItem(Object item, int i) {
    if (i < 0 || i > length) {
        throw new IndexOutOfBoundsException();
    } else if (isFull()) {
        return false;
    }

    // make room for the new item
    for (int j = length - 1; j >= i; j--) {
        items[j + 1] = items[j];
    }

    items[i] = item;
    length++;
    return true;
}

/* 
 * removeItem - removes the item at position i in the list,
 * shifting the items that are currently in positions i+1, i+2,
 * etc. to the left by one.  Returns a reference to the removed
 * object.
 */
public Object removeItem(int i) {
    if (i < 0 || i >= length) {
        throw new IndexOutOfBoundsException();
    }

    Object removed = items[i];

    // fill in the "hole" left by the removed item
    for (int j = i; j < length - 1; j++) {
        items[j] = items[j + 1];
    }
    items[length - 1] = null;

    length--;
    return removed;
}

/*
 * toString - converts the list into a String of the form 
 * {item0, item1, ...}
 */
public String toString() {
    String str = "{";

    for (int i = 0; i < length; i++) {
        str = str + items[i];
        if (i < length - 1) {
            str = str + ", ";
        }
    }

    str = str + "}";
    return str;
}

/*
 * iterator - returns an iterator for this list
 */
public ListIterator iterator() {
    // still needs to be implemented
    return null;
}

为了帮助您更多,这里有一个简单的解决方案(可能会更有效),它利用了给定的removeItem函数。 只需阅读并尝试理解它,否则可能会遇到麻烦,如果对此有疑问,请随时提出。

public void removeAll(Object o) {
    int i = 0;
    while (i < length) {
        if (o.equals(items[i])) {
            removeItem(i);
        } else {
            i++;
        }
    }
}

并对其进行测试:

public static void main(String[] args) {

    ArrayList list = new ArrayList(10);
    list.addItem("1", 0);
    list.addItem("3", 1);
    list.addItem("2", 2);
    list.addItem("2", 3);
    list.addItem("4", 4);
    list.addItem("1", 5);
    list.addItem("2", 6);

    System.out.println(list);
    // {1, 3, 2, 2, 4, 1, 2}

    list.removeAll("2");

    System.out.println(list);
    // {1, 3, 4, 1}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM