We always convert {@link Iterable} to an array to avoid the cost of creating
a new {@Link java.util.Iterator} object each time the parser runs.
354802
@SuppressWarnings({"unchecked", "rawtypes"})
  @Private static <T> Parser<T>[] toArray(Iterable<? extends Parser<? extends T>> parsers) {
    if (parsers instanceof Collection<?>) {
      return toArray((Collection) parsers);
    }
    return toArrayWithIteration(parsers);
  }
240831
public static <E> Iterable<E> asIterable(final Iterator<E> iter) {
		return IterUtil.asIterable(iter);
	}
609439
@Override
  public long[] toArray() {
    long cardinality = this.getLongCardinality();
    if (cardinality > Integer.MAX_VALUE) {
      throw new IllegalStateException("The cardinality does not fit in an array");
    }

    final long[] array = new long[(int) cardinality];

    int pos = 0;
    LongIterator it = getLongIterator();

    while (it.hasNext()) {
      array[pos++] = it.next();
    }
    return array;
  }
475945
@GwtIncompatible("Array.newArray(Class, int)")
  public final E[] toArray(Class<E> type) {
    return Iterables.toArray(iterable, type);
  }
1070622
public Object[] toArray() {
    Iterator<E> iter = iterator();
    ArrayList<Object> al = new ArrayList<Object>();
    while (iter.hasNext()) {
      al.add(iter.next());
    }
    return al.toArray();
  }
72564
public static <T> Map<Integer, T> fromIterable(
        Iterable<? extends T> iterable)
    {
        Map<Integer, T> map = new LinkedHashMap<Integer, T>();
        int i = 0;
        for (T t : iterable)
        {
            map.put(i, t);
            i++;
        }
        return map;
    }
1195355
private static Iterable<String> toIterable(final String[] strings) {
        if (strings == null) {
            return null;
        }

        return Arrays.asList(strings);
    }
802244
public Iterator<Example> iterator()
    {
        return new Iterator<Example>()
        {
            private int index = 0;

            public boolean hasNext()
            {
                return at( index ) != null;
            }

            public Example next()
            {
                return at( index++ );
            }

            public void remove()
            {
                throw new UnsupportedOperationException( "Specification not mutable" );
            }
        };
    }
575734
public static <T> List<T> toList(Iterator<T> iter) {
    List<T> buf = new ArrayList<>();
    while (iter.hasNext()) {
      buf.add(iter.next());
    }
    return buf;
  }
314579
public static <T> List<T> toList(Iterable<T> iterable)
    {
        Objects.requireNonNull(iterable, "The iterable is null");
        return Iterables.toCollection(iterable, new ArrayList<T>());
    }
686880
public @Nonnull Iterable<JsonObject> objects() {
        final JsonArray parent=this;
        return () -> {
            final Iterator<JsonElement> iterator = parent.iterator();
            return new Iterator<JsonObject>() {

                @Override
                public boolean hasNext() {
                    return iterator.hasNext();
                }

                @Override
                public JsonObject next() {
                    return iterator.next().asObject();
                }

                @Override
                public void remove() {
                    iterator.remove();
                }
            };
        };
    }
110851
public static <T> C.List<T> toList(Iterator<? extends T> iterator) {
        ListBuilder<T> lb = new ListBuilder<T>(10);
        lb.append(iterator);
        return lb.toList();
    }
499098
public static function iterableToArray($iterable): array
    {
        if (is_array($iterable)) {
            return $iterable;
        }

        if ($iterable instanceof \Traversable) {
            return iterator_to_array($iterable);
        }

        throw new \InvalidArgumentException('Iterable is expected.');
    }
3554
private Iterator<Object> arrayIt(final Object array) {
        return new Iterator<Object>() {
            private int index = 0;
            private final int length = Array.getLength(array);
            @Override
            public boolean hasNext() {
                return this.index < this.length;
            }
            @Override
            public Object next() {
                try {
                    return Array.get(array, this.index++);
                } catch (Throwable t) {
                    throw new NoSuchElementException(t.getMessage());
                }
            }
        };
    }
1204521
public Iterator<String> iterator() throws IOException
  {
    String list[] = list();

    // Avoids NPE when subclasses override list() and
    // possibly return null, e.g. JarPath.
    if (list == null)
      list = new String[0];

    return new ArrayIterator(list);
  }
167109
public static <T> ArrayList<T> newArrayList(Iterator<T> iter) {
		return (ArrayList<T>) list(false, iter);
	}
566867
public static Iterable<Object> subIterable(Object iterable, int fromIndex) {
    if (iterable == null || getLength(iterable) < fromIndex)
      return Collections.emptyList();

    final Iterator<Object> iterator = iterator(iterable);
    for (int i = 0; i < fromIndex; i++)
      iterator.next();
    return new Iterable<Object>() {
      @Override
      public Iterator<Object> iterator() {
        return iterator;
      }
    };
  }