Re: Newbie: iterating two collections



On Oct 30, 3:34 pm, "u...@xxxxxxxxx" <u...@xxxxxxxxx> wrote:
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?

Ok so what I understand you want is an iterator that will set all
boolean values in list B to be the same as list A. I am assuming the
type of list B (T) is actually Boolean, if not you can change the
setter. As far as the iterator, you were close.

Here is a simple example:

for(int i; i<A.size(); i++)
{
B.set(i, A.get(i));
}

This assumes the List B is already has elements which you're replacing
with the elements in A. If List B is empty you could just use the add
method instead:

for(int i; i<A.size(); i++)
{
B.add(A.get(i));
}

Scott

.



Relevant Pages

  • Re: Difference between Java iterator and iterator in Gang of Four
    ... I love how they phrased it: "some implementations". ... >> Iterator is part of Collection that does not ... > efficient way to initialize a new Iterator properly, ... "(return a boolean) or void". ...
    (comp.lang.java.programmer)
  • Re: Difference between Java iterator and iterator in Gang of Four
    ... Dimitri Maziuk wrote: ...
    (comp.lang.java.programmer)
  • Re: Difference between noditerator.movenext and...
    ... VS.net is letting it compile for me. ... > MoveNext() method moves iterator to a next node and returns boolean ... It returns a boolean. ...
    (microsoft.public.dotnet.xml)
  • Re: Basic question
    ... Although the python for/in loop is superficially similar to C and Java ... object that can create an iterator, and the for/in construct under the ... iterator on potentially infinite lists, streams, and generators. ...
    (comp.lang.python)
  • Re: Sacla loop: a new loop implementation
    ... >> no iterator object needs to be made? ... > that either you cons an iterator or you cons a closure. ... be slow (just as you do in more `static' languages like Java in fact). ... But sometimes, you do have type information available at compile time, ...
    (comp.lang.lisp)