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?
heysc0tt@gmail.com - 31 Oct 2006 01:14 GMT
On Oct 30, 3:34 pm, "u...@yahoo.com" <u...@yahoo.com> 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
Mark Jeffcoat - 31 Oct 2006 01:59 GMT
> 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.

Signature
Mark Jeffcoat
Austin, TX
tej - 31 Oct 2006 06:02 GMT
Hi all
i am working on java platform
i ve to parese a textfile containing Japanese charaters can any body
help in this problem
Thanks& Regards
Tej
Michael Rauscher - 31 Oct 2006 17:37 GMT
tej schrieb:
> Hi all
> i am working on java platform
> i ve to parese a textfile containing Japanese charaters can any body
> help in this problem
Wrong thread but: text files are binary files, too.
The first thing you need to know: which encoding was used to generate
that file?
The second thing: does Java support this encoding out of the box? If e.
g. the file is UTF-16 encoded, then: yes.
The last thing: create an InputStreamReader that uses the encoding.
So, e. g. to read an UTF-16 encoded file:
BufferedReader reader = new BufferedReader( new InputStreamReader(
new FileInputStream("myfile.txt"), "UTF-16") );
Bye
Michael
Thomas Hawtin - 31 Oct 2006 17:09 GMT
> 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?
A few points:
Variables should start with a lower case letter.
Boolean is immutable. You cannot change the actual object, but you can
change references to a Boolean (so long as the references are not final).
It's a good idea to avoid non-private fields.
You need to specify that T is of some concrete type with the members
that your code requires. In this example you can use wildcards.
It's probably better to use the original list, rather than having two
copies of the same thing.
Instead of List<Boolean> you may find BitSet preferable, particularly as
it will be around 32 or 64 times as compact (depending on whether your
JVM is using 32 or 64 bit addresses).
If you start with 'A' as an empty List, you don't need to worry about an
iterator for it.
So, ignoring the BitSet and using a single List:
interface BooleanThing {
boolean isBooleanValue();
}
public static List<Boolean> extractBooleans(
Iterable<? extends BooleanThing> things
) {
List<Boolean> booleans = new ArrayList<Boolean>(objects.size());
for (BooleanThing thing : things) {
booleans.add(thing.isBooleanValue());
}
return booleans;
}
Obviously the names aren't very good as I don't know why you are doing this.
Tom Hawtin