Java Forum / General / January 2007
parallel for
gaurav v bagga - 16 Jan 2007 14:34 GMT hi all,
if i have two arrays of same size and want to evaluate both of them in parallel using
for(String s :<String array>){ }
how to do this
i'll appreciate if anyone helps me with this.....
regards gaurav
Thomas Fritsch - 16 Jan 2007 15:52 GMT > if i have two arrays of same size and want to evaluate both of them in > parallel using [quoted text clipped - 3 lines] > > how to do this ? AFAIK the new for-syntax with ':' is not capable of doing with 2 arrays in parallel. Therefore I would do it the old-fashioned way with an index i: String[] array1 = { "abc", "defg" "hi" }; String[] array2 = { "12", "345", "6789" }; for (int i = 0; i < array1.length; i++) { String s1 = array1[i]; String s2 = array2[i]; //.... }
 Signature Thomas
Remon van Vliet - 16 Jan 2007 16:05 GMT > hi all, > [quoted text clipped - 10 lines] > regards > gaurav you cannot do so with the for-each syntax, not is there a way to get the index of the element related to the current iteration in a for-each loop.
Remon
Chris Uppal - 16 Jan 2007 16:48 GMT > if i have two arrays of same size and want to evaluate both of them in > parallel using [quoted text clipped - 3 lines] > > how to do this You can't. You'll have to use the "normal" looping syntax.
for (int i = 0; i < array1.length; i++) { String s1 = array1[i]; String s2 = array2[i]. .... }
-- chris
John Ersatznom - 16 Jan 2007 17:21 GMT > hi all, > [quoted text clipped - 7 lines] > > i'll appreciate if anyone helps me with this..... So far as I'm aware, there's no tidy new syntax for double-barreled iteration, so it's the good old fashioned:
for (int i = 0; i < index; ++i) { doSomething(array1[i], array2[i]); }
and:
for (Iterator i = collection1.iterator(), j = collection2.iterator(); i.hasNext() && j.hasNext();) { doSomething(i.next(), j.next()); }
Yeah, I know, yuck. (The latter calls something for successive pairs from any two Collections, even if unequal in size; in that case stopping when the smaller one has been iterated completely. The pairs are only likely to be meaningful for List and LinkedHashSet, which preserve ordering, and maybe SortedFoos.)
When you have same-sized collections that you tandem-iterate, it's generally one of two cases:
Either you're doing vector arithmetic in disguise (indicated when the lists are interchangeable) and you want to wrap all this in an object implementing the vector arithmetic; or
You are really working with a single list of tuples of some kind (indicated when the lists are distinct and tandem iterations use specific pairs of lists all the time). Example is tandem-iterating listX and listY and doing something with (x, y) pairs.
In the latter case, you really want to refactor: create an object encapsulating the logical records (e.g. (x, y) pairs) and a single list of these objects. If the objects really have no behavior of their own, just make them plain old data -- some public (probably final) fields and maybe a constructor. But do think about at least hiding the fields and providing accessors, if not actually giving them more interesting behavior. If any of your lists hold special flag values or type codes, that's a sure sign that you should not only combine the data into a class and put instances in a single list, but actually make that class have subclasses and polymorphic behavior. This comes up most frequently when porting legacy code to OO, or using a familiar procedural-language or functional-language idiom in an OO environment. Leverage polymorphism to do some of the heavy lifting!
Daniel Pitts - 16 Jan 2007 18:45 GMT > hi all, > [quoted text clipped - 10 lines] > regards > gaurav A few approaches, This first approach is the better designed approach in my opinion. If you have to values that are associated with eachother, then they most likely belong in the same object.
Take String[] a; Object[] b;
and refactor to:
class Entry { String a; Object b; };
Entry[] entries;
Then you can use
for (Entry entry: entries) { // do stuff with entry.a, entry b } --- Or, you can use for (int i = 0; i < a.length && i < b.length; ++i) { String aValue = a[i]; Object bValue = b[i]; } --- Alternatively, you can create a "class ParallelArrayView<T1, T2> implements Iterable<Entry<T1,T2>>", which takes the two arrays in a constructor. You then right an interator that will access the two arrays in parallel, and return an new Entry object with the currect paring. --- If you really need to do it with for each, you can use Python instead of Java. for <aValue, bValue> in zip(a, b): # do stuff with aValue and bValue Note, this is basically the same thing as the ParallelArrayView class I showed you above. --- I suggest using one of the first two methods, and ignoring the last two. Hope this helps, Daniel.
gaurav v bagga - 17 Jan 2007 05:41 GMT hi all,
thanks for reply and views and refactoring thing, i dint think of putting the two lists into one class,that will actually help.. the thing is i have two list coming from different sources 1)is statically typed 2)is user input which i have to match with 1
eg:
1)"a=?,b=?,c=?" 2)String a,b,c;
then i split 1) and match with 2)
array1 "a=","b=","c=" array2 a,b,c
finally
finally constructed string "a=<a>","b=<b>".......... any more guidelines???
regards gaurav
gaurav v bagga - 17 Jan 2007 12:36 GMT hi,
is this a good way to match pairs
String str="a= ? , b= 2 , c= ?"; String s2[]={"a","b"};
for(String s:s2){ str=str.replaceFirst("[?]", s); }
=>str="a=a,b=2,c=b"
regards gaurav
Andreas Leitgeb - 17 Jan 2007 16:56 GMT > is this a good way to match pairs > String str="a= ? , b= 2 , c= ?"; [quoted text clipped - 3 lines] > } >=>str="a=a,b=2,c=b" This situation reminds me of java.util.Formatter
The only thing needed in advance is to replace each question mark by a "%s".
If you can change the source of "a= ? , b= 2 , c= ?", to use %s rather than ?, then your life would be made again easier.
gaurav v bagga - 18 Jan 2007 05:38 GMT hi,
well i cannot change ? to %s as ? marks symbolizes more meaningful sentence in my situation then %s would :(
method => search("a=? and b=? and c=friend",a,b,c) search(String,String...arr)
one question you mentioned "your life would be made again easier" can you explain scenarios where ? can create problems..?
regards gaurav
Andreas Leitgeb - 18 Jan 2007 10:26 GMT > hi, > well i cannot change ? to %s as ? marks symbolizes more meaningful > sentence in my situation then %s would :( To use the methods from Formatter, you need %s as placeholders. You can use String's replace(...) to convert "?" -> "%s" on the fly, and then use Formatter.
> one question you mentioned > "your life would be made again easier" can you explain scenarios > where ? can create problems..? With that, I meant that if you could change to %s generally, (which you now wrote you can't), then you could have saved the on-the-fly conversion, and thus "made your life easier". (making life easier is something positive)
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|