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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » ListScreenshots 
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 (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 += ((Listo).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 = (Listit.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 = (Listit.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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.