Error in method - using Iterator to interweave a List

前端 未结 2 410
甜味超标
甜味超标 2021-01-15 01:03

I\'ve created a method that should interweave two list objects and return the new, inter-weaved, list.

i.e. If aList is [A,C,E,G] & bList is [B, D, F] the metho

相关标签:
2条回答
  • 2021-01-15 01:29

    I believe this can be done in a more simple way, using while(iterator.hasNext()) idiom:

    itrA = a.iterator();
    itrB = b.iterator(); 
    
    while (itrA.hasNext() || itrB.hasNext()) {
      if (itrA.hasNext())
          newList.add(itrA.next());
      if (itrB.hasNext())
          newList.add(itrB.next());
    }
    
    0 讨论(0)
  • 2021-01-15 01:42

    It is possible to compose the lists dynamically:

    static class InterWeave<T> implements Iterable<T> {
    
        // The lists to be interwoven.
        final Iterable<T>[] lists;
    
        public InterWeave(Iterable<T>... lists) {
            this.lists = lists;
        }
    
        @Override
        public Iterator<T> iterator() {
            return new WeaveIterator();
    
        }
    
        private class WeaveIterator implements Iterator<T> {
    
            // Which list to offer next.
            int whichList = 0;
            // The Iterators of those lists.
            final List<Iterator<T>> iterators = new ArrayList(lists.length);
            // The current iterator.
            Iterator<T> i;
            // The next to deliver - null menas not primed yet.
            T next = null;
    
            public WeaveIterator() {
                // Take some iterators.
                for (Iterable<T> l : lists) {
                    if (l != null) {
                        iterators.add(l.iterator());
                    }
                }
            }
    
            @Override
            public boolean hasNext() {
                // Have we already prepared one?
                if (next == null) {
                    // Grab the next iterator and step on.
                    i = iterators.get(whichList++);
                    // detect that we're back at start.
                    Iterator<T> start = i;
                    // Cycle around the iterators.
                    whichList %= iterators.size();
                    while (!i.hasNext()) {
                        // Get next one.
                        i = iterators.get(whichList++);
                        whichList %= iterators.size();
                        if (i == start) {
                            // Stop here if we've gone all the way around.
                            break;
                        }
                    }
                    if (i.hasNext()) {
                        next = i.next();
                    }
                }
                return next != null;
            }
    
            @Override
            public T next() {
                if (hasNext()) {
                    T n = next;
                    // Mark it used.
                    next = null;
                    return n;
                } else {
                    throw new NoSuchElementException();
                }
            }
    
        }
    
    }
    
    public void test() {
        List<String> a = Arrays.asList(new String[]{"A", "C", "E", "G"});
        List<String> b = Arrays.asList(new String[]{"B", "D", "F"});
        for (String s : new InterWeave<String>(a, b)) {
            System.out.println(s);
        }
    }
    
    0 讨论(0)
提交回复
热议问题