Re: Newbie: iterating two collections



"usgog@xxxxxxxxx" <usgog@xxxxxxxxx> writes:

I have two ArrayLists: List<Boolean> A and List<T> B with same size. I
want something like
for (int i =0; i < A.size(); i++) { B[i].booleanValue =
A[i].booleanValue); }. Just one to one value set. I am pretty new to
iterators. Can I use foreach in java for that? Any decent approach
instead of nested loops?



If I've understood you correctly, this is one of those
times when the new iterator syntax is a bit awkward. What
you wrote almost works. (The booleanValue bit is not valid syntax,
though. It's a method call (or should be), and you can't assign to it.)

public T someMethod(Boolean b) { .... }

List<Boolean> a = ...;
List<T> b = ... ;

assert(a.size() == b.size());
for (int i=0; i<a.size(); ++i) {
b.set(i, someMethod(a.get(i)));
}

Fill in the blanks as makes you happy. Get() and set() are
specific to Lists; you will need Iterators for more general
Collections.

--
Mark Jeffcoat
Austin, TX
.



Relevant Pages

  • Newbie: iterating two collections
    ... iterators. ... Can I use foreach in java for that? ... Any decent approach ...
    (comp.lang.java.programmer)
  • Re: iterators
    ... {public boolean IsAvailable(){... ... It gets more complicated than that if you consider different classes of iterators (ListIterators in Java for example, or Random Access Iterators in C++) ...
    (comp.lang.java.programmer)
  • Re: OO Style with Ada Containers
    ... cursor/iterator and this is different from both STL and Java (STL ... anyway - in both cases iterators can be used to query as well as to ... algorithms work on whole containers. ...
    (comp.lang.ada)