⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise17_1.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
public class Exercise17_1 {
  public static void main(String[] args) {
    MyArrayList list1 = new MyArrayList(new String[] {"Tom", "George",
      "Peter", "Jean", "Jane"});
    MyArrayList list2 = new MyArrayList(new String[] {"Tom", "George",
      "Michael", "Michelle", "Daniel"});

    list1.addAll(list2);
    System.out.println("Union is : " + list1);

    list1 = new MyArrayList(new String[] {"Tom", "George",
      "Peter", "Jean", "Jane"});
    list1.removeAll(list2);
    System.out.println("Difference is : " + list1);

    list1 = new MyArrayList(new String[] {"Tom", "George",
      "Peter", "Michael", "Jean", "Jane"});
    list1.retainAll(list2);
    System.out.println("Intersection is : " + list1);
  }
}

// MyAbstractList.java: Partial implementation of MyList
// Added three new set operators in the exercise.
abstract class MyAbstractList implements MyList {
  protected int size;

  /** Create a default list */
  protected MyAbstractList() {
  }

  /** Create a list from an array of objects */
  protected MyAbstractList(Object[] objects) {
    for (int i = 0; i < objects.length; i++)
      this.add(objects[i]);
  }

  /** Add a new element o at the end of this list */
  public void add(Object o) {
    add(size, o);
  }

  /** Return true if this list contains no elements */
  public boolean isEmpty() {
    return size == 0;
  }

  /** Return the number of elements in this list */
  public int size() {
    return size;
  }

  /** Add the elements in otherList to this list.
  * Returns true if this list changed as a result of the call */
 public boolean addAll(MyList otherList) {
   boolean changed = false;

   for (int i = 0; i < otherList.size(); i++) {
     if (!this.contains(otherList.get(i))) {
       this.add(otherList.get(i));
       changed = true;
     }
   }

   return changed;
 }

 /** Remove all the elements in otherList from this list
  * Returns true if this list changed as a result of the call */
 public boolean removeAll(MyList otherList) {
   boolean changed = false;

   for (int i = 0; i < otherList.size(); i++) {
     if (this.contains(otherList.get(i))) {
       this.remove(otherList.get(i));
       changed = true;
     }
   }

   return changed;
 }

 /** Retain the elements in this list if they are also in otherList
  * Returns true if this list changed as a result of the call */
 public boolean retainAll(MyList otherList) {
   boolean changed = false;

   for (int i = 0; i < this.size(); ) {
     if (!otherList.contains(this.get(i))) {
       this.remove(this.get(i));
       changed = true;
     }
     else
       i++;
   }

   return changed;
 }
}

// MyArrayList.java: List implementation using an array
class MyArrayList extends MyAbstractList {
  private final int INITIAL_CAPACITY = 16;
  private Object[] data;

  /** Create a default list */
  public MyArrayList() {
    data = new Object[INITIAL_CAPACITY];
  }

  /** Create a list from an array of objects */
  public MyArrayList(Object[] objects) {
    data = objects;
    size = objects.length;
  }

  /** Add a new element o at the specified index in this list */
  public void add(int index, Object o) {
    ensureCapacity();

    // Move the elements to the right after the specified index
    for (int i = size - 1; i >= index; i--)
      data[i + 1] = data[i];

    // Insert new element to data[index]
    data[index] = o;

    // Increase size by 1
    size++;
  }

  /** Create a new larger array, double the current capacity */
  private void ensureCapacity() {
    if (size >= data.length) {
      Object[] newData = new Object[data.length * 2];
      System.arraycopy(data, 0, newData, 0, data.length);
      data = newData;
    }
  }

  /** Clear the list */
  public void clear() {
    data = new Object[INITIAL_CAPACITY];
  }

  /** Return true if this list contains the element o */
  public boolean contains(Object o) {
    for (int i = 0; i < size; i++)
      if (o.equals(data[i])) return true;

    return false;
  }

  /** Return the element from this list at the specified index */
  public Object get(int index) {
    return data[index];
  }

  /** Return the index of the first matching element in this list.
   *  Return -1 if no match. */
  public int indexOf(Object o) {
    for (int i = 0; i < size; i++)
      if (o.equals(data[i])) return i;

    return -1;
  }

  /** Return the index of the last matching element in this list
   *  Return -1 if no match. */
  public int lastIndexOf(Object o) {
    for (int i = size - 1; i >= 0; i--)
      if (o.equals(data[i])) return i;

    return -1;
  }

  /** Remove the first occurrence of the element o from this list.
   *  Shift any subsequent elements to the left.
   *  Return true if the element is removed. */
  public boolean remove(Object o) {
    for (int i = 0; i < size; i++)
      if (o.equals(data[i])) {
        remove(i);
        return true;
      }

    return false;
  }

  /** Remove the element at the specified position in this list
   *  Shift any subsequent elements to the left.
   *  Return the element that was removed from the list. */
  public Object remove(int index) {
    Object o = data[index];

    // Shift data to the left
    for (int j = index; j < size - 1; j++)
      data[j] = data[j + 1];

    // Decrement size
    size--;

    return o;
  }

  /** Replace the element at the specified position in this list
   *  with the specified element. */
  public Object set(int index, Object o) {
    data[index] = o;
    return o;
  }

  /** Override toString() to return elements in the list */
  public String toString() {
    StringBuffer result = new StringBuffer("[");

    for (int i = 0; i < size; i++) {
      result.append(data[i]);
      if (i < size - 1) result.append(", ");
    }

    return result.toString() + "]";
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -