Java tutorial
//package com.java2s; import java.util.Iterator; public class Main { public static <E> String toString(Iterator<E> iterator) { if (iterator == null || !iterator.hasNext()) { return "[]"; } StringBuilder builder = new StringBuilder(); builder.append('['); while (iterator.hasNext()) { builder.append(iterator.next()).append(',').append(' '); } return builder.append(']').toString(); } }