Pages

Friday, 3 February 2017

Chapter 24 Exercise 15, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

24.15(Test MyArrayList ) Design and write a complete test program to test if the
MyArrayList class in Listing 24.3 meets all requirements.


public class MyArrayList<E> extends MyAbstractList<E> {
 public static final int INITIAL_CAPACITY = 16;
 private E[] data = (E[]) new Object[INITIAL_CAPACITY];

 /** Create a default list */
 public MyArrayList() {
 }

 /** Create a list from an array of objects */
 public MyArrayList(E[] objects) {
  for (int i = 0; i < objects.length; i++)
   add(objects[i]);
 }

 @Override /** Add a new element at the specified index */
 public void add(int index, E e) {
  ensureCapacity();

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

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

  // Increase size by 1
  size++;
 }

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

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

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

  return false;
 }

 @Override /** Return the element at the specified index */
 public E get(int index) {
  checkIndex(index);
  return data[index];
 }

 private void checkIndex(int index) {
  if (index < 0 || index >= size)
   throw new IndexOutOfBoundsException("index" + index + " out of bounds");
 }

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

  return -1;
 }

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

  return -1;
 }

 @Override /** 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 E remove(int index) {
  checkIndex(index);

  E e = data[index];

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

  data[size - 1] = null; // This element is now null

  // Decrement size
  size--;

  return e;
 }

 @Override /** Replace the element at the specified position
  * in this list with the specified element. */
 public E set(int index, E e) {
  checkIndex(index);
  E old = data[index];
  data[index] = e;
  return old;
 }

 @Override 
 public String toString() {
  StringBuilder result = new StringBuilder("[");

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

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

 /** Trims the capacity to current size */
 public void trimToSize() {
  if (size != data.length) {
   E[] newData = (E[])(new Object[size]);
   System.arraycopy(data, 0, newData, 0, size);
   data = newData;
  } // If size == capacity, no need to trim
 }

 @Override /** Override iterator() defined in Iterable */
 public java.util.Iterator<E> iterator() {
  return new ArrayListIterator();
 }

 private class ArrayListIterator
   implements java.util.Iterator<E> {
  private int current = 0; // Current index

  @Override
  public boolean hasNext() {
   return (current < size);
  }

  @Override
  public E next() {
   return data[current++];
  }

  @Override
  public void remove() {
   MyArrayList.this.remove(current);
  }
 }
}
Solution: Program Flow:
• Create an ArrayListTesting class for test the MyArrayList class.
• This MyArrayList class extends MyAbstractList class and that class implements MyList interface. These three classes are referring to the book.
• In main() method of ArrayListTesting create an object of the MyArrayList class
• In ArrayListTesting class, call add()methods is used to insert elements at specified index of the array list.
• remove () method used to remove elements from the array list elements at specified index.
• Call set() return type method to replace list elements by entered data at the specified index position.
• Call contains() Boolean type method to check if your entered name is in list then it returns true else returns false.
• Call get() return type method to check if your entered index value is in list then it returns element at the specified index.
• Call indexOf()method to check if your entered name is in list then it returns index else returns -1.
• Call lastIndexOf() method to check if your entered name is in list then it returns index else returns -1.
• Call trimToSize() method, it checks the size of the array if size is not equal to the capacity it’s copy all elements to new array list else if size is equals to the capacity then there is no trim to it.
• Create an object of Iterator, call hasNext() return type method, it returns true if at least one elements is in list. Call next() return type method, it returns true if current element is in list. Call remove() method, it remove size of the list.
• Call clear() method to remove all the list elements from the array list.

import java.util.Iterator;
//Create a ArrayListTesting
public class ArrayListTesting {
 // declare main() method
 public static void main(String args[]) {
  //create list object of MyArrayList
  MyList list = new MyArrayList();
  //insert String value in array list
  list.add("Mango");
  System.out.println("add String: " + list);
  //insert String value in array list
  list.add("Grapes");
  System.out.println("add String: " + list);
  //insert String value in array list
  list.add("Papaya");
  System.out.println("add String: " + list);
  //insert String value in array list
  list.add(0, "Orange");
  System.out.println("add String: " + list);
  list.add(1, "Banana");
  System.out.println("add String: " + list);
  //insert String value in array list
  list.add(2, "Guava");
  System.out.println("add String: " + list);
  //insert String value in array list
  list.add(0, "Apple");
  System.out.println("add String: " + list);
  // Set the element at index 1
  list.set(1, "Pineapple");
  System.out.println("set String: " + list);
  //remove String value from array list
  list.remove("Guava");
  System.out.println("Remove String: " + list);
  //remove String value at specified index
  list.remove(3);
  System.out.println("Remove String: " + list);
  //remove String value of last index value
  list.remove(list.size() - 1);
  System.out.println("Remove String: " + list);
  //Print remaining values
  System.out.print("Remaining String: ");
  //print all the remaining elements
  System.out.print(list + ", ");
  // Call contains() method to know
  // elements are in list or not
  System.out.println("\ncontains elements Apple: " + list.contains("Apple"));
  // Call get() method to retrieve
  // element at specified index
  System.out.println("get element at 1 index: " + list.get(1));
  // Call indexOf() method to get
  // the index of specified element.
  System.out.println("index Of element Banana: " + list.indexOf("Banana"));
  // Call lastIndexOf() method to get
  // the index of specified element.
  System.out.println("last index Of " + "Pineapple element: " + list.lastIndexOf("Pineapple"));
  System.out.println("check the size of the " + "arrayby calling trimToSize() method.");
  ((MyArrayList) list).trimToSize();
  // Create an object of Iterator
  Iterator itr = ((MyArrayList) list).iterator();
  // Call hasNext() method to check
  // if any element in list it returns true
  System.out.println("hasNext: " + itr.hasNext());
  // Call next() method, it returns true
  // if current element is in list
  System.out.println("next: " + itr.next());
  System.out.println("remove size of list: ");
  // Call remove() method to remove size
  itr.remove();
  // Call clear() method to remove
  // all elements form the list
  list.clear();
  System.out.print("All elements " + "are clear now. ");
 }
}

Output:
add String: [Mango]
add String: [Mango, Grapes]
add String: [Mango, Grapes, Papaya]
add String: [Orange, Mango, Grapes, Papaya]
add String: [Orange, Banana, Mango, Grapes, Papaya]
add String: [Orange, Banana, Guava, Mango, Grapes, Papaya]
add String: [Apple, Orange, Banana, Guava, Mango, Grapes, Papaya]
set String: [Apple, Pineapple, Banana, Guava, Mango, Grapes, Papaya]
Remove String: [Apple, Pineapple, Banana, Mango, Grapes, Papaya]
Remove String: [Apple, Pineapple, Banana, Grapes, Papaya]
Remove String: [Apple, Pineapple, Banana, Grapes]
Remaining String: [Apple, Pineapple, Banana, Grapes],
contains elements Apple: true
get element at 1 index: Pineapple
index Of element Banana: 2
last index Of Pineapple element: 1
check the size of the array by calling trimToSize() method.
hasNext: true
next: Apple
remove size of list:All elements are clear now.

No comments:

Post a Comment