List containing other lists : List « Collections Data Structure « Java






List containing other lists

     
/*
 * $Id: ListOfLists.java,v 1.1.1.1 2005/04/07 18:36:24 pocho Exp $
 */


import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Creates list containing other lists, access another list's elements as it would belong
 * to this list. 
 * 
 * @version $Name:  $ - $Revision: 1.1.1.1 $ - $Date: 2005/04/07 18:36:24 $
 * TODO Test
 */
public class ListOfLists extends AbstractList {
  private Collection lists = new ArrayList();

  public ListOfLists(Collection c) {
    Iterator it = c.iterator();
    Object o;
    while (it.hasNext()) {
      o = it.next();
      if (o instanceof List)
        lists.add(o);
      else if (o != null)
        throw new UnsupportedOperationException(this.getClass().getName() +
                                                " class supports only instances "+
                                                "of java.util.List interface");
    }
  }

  public int size() {
    Iterator it = lists.iterator();
    int size = 0;
    Object o;
    while (it.hasNext()) {
      o = it.next();
      if (o != null)
        size += ((List) o).size();
    }
    return size;
  }

  public Object get(int index) {
    int size = size();
    if (index < 0)
      throw new IndexOutOfBoundsException("index: " + index +"; size: " + size);

    Iterator it = lists.iterator();
    while (it.hasNext()) {
      List list = (List) it.next();
      if (index < list.size()) {
        return list.get(index);
      }
      else
        index -= list.size();
    }

    // if value has not been returned yet - IndexOutOfBoundsException is thrown
    throw new IndexOutOfBoundsException("index: " + index +"; size: " + size);
  }

  /**
   * Replaces the element at the specified position in underlying list with the
   * specified element.
   *
   * @param index index of element to replace.
   * @param element element to be stored at the specified position.
   * @return the element previously at the specified position.
   */
  public Object set(int index, Object element) {
    int size = size();
    if (index < 0)
      throw new IndexOutOfBoundsException("index: " + index +"; size: " + size);

    Iterator it = lists.iterator();
    while (it.hasNext()) {
      List list = (List) it.next();
      if (index < list.size()) {
        return list.set(index, element);
      }
      else
        index -= list.size();
    }

    // if value has not been returned yet - IndexOutOfBoundsException is thrown
    throw new IndexOutOfBoundsException("index: " + index +"; size: " + size);
  }

  public int indexOf(Object o) {
    ListIterator e = listIterator();
    if (o==null) {
      while (e.hasNext()) {
        if (e.next() == null)
          return e.previousIndex();
      }
    }
    else {
      Object el;
      while (e.hasNext()) {
        el = e.next();
        if (el.equals(o))
          return e.previousIndex();
      }
    }
    return -1;
  }

  public int lastIndexOf(Object o) {
    ListIterator e = listIterator(size());
    if (o==null) {
      while (e.hasPrevious()) {
        if (e.previous() == null)
          return e.nextIndex();
      }
    } else {
      Object el;
      while (e.hasPrevious()) {
        el = e.previous();
        if (el != null && el.equals(o))
          return e.nextIndex();
      }
    }
    return -1;
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Using the Double Brace Initialization.
2.Add to end Performance compare: LinkList and ArrayListAdd to end Performance compare: LinkList and ArrayList
3.Add to start Performance compare: LinkList and ArrayList
4.Convert array to list and sortConvert array to list and sort
5.Shuffle a listShuffle a list
6.Sort a listSort a list
7.Bidirectional Traversal with ListIteratorBidirectional Traversal with ListIterator
8.Int listInt list
9.Linked List example
10.List to array
11.List Reverse Test
12.Build your own Linked List class
13.List Search Test List Search Test
14.Convert a List to a Set
15.Set Operating on Lists: addAll, removeAll, retainAll, subList
16.Convert collection into array
17.Convert LinkedList to array
18.Convert Set into List
19.If a List contains an item
20.ListSet extends List and Set
21.Helper method for creating list
22.Generic to list
23.List implementation with lazy array construction and modification tracking.
24.Utility methods for operating on memory-efficient lists. All lists of size 0 or 1 are assumed to be immutable.
25.A class that wraps an array with a List interface.
26.Splits the list.
27.Slice a list
28.A List that, like a Set, contains no duplicate Elements.
29.Determines if the given lists contain the same elements. We suppose that all the elements of the given lists are different.
30.List that allows items to be added with a priority that will affect the order in which they are later iterated over.