Re: Newbie: iterating two collections
- From: Mark Jeffcoat <jeffcoat@xxxxxxxxxxxxxxx>
- Date: Tue, 31 Oct 2006 00:59:13 GMT
"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
.
- References:
- Newbie: iterating two collections
- From: usgog@xxxxxxxxx
- Newbie: iterating two collections
- Prev by Date: Re: mutate an object or create a new one?
- Next by Date: Re: Design/Inheritance Question
- Previous by thread: Re: Newbie: iterating two collections
- Next by thread: Re: Newbie: iterating two collections
- Index(es):
Relevant Pages
|