parallel for

G

gaurav v bagga

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
 
T

Thomas Fritsch

gaurav said:
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 ?
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;
String s2 = array2;
//....
}
 
R

Remon van Vliet

gaurav v bagga said:
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

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
 
C

Chris Uppal

gaurav said:
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

You can't. You'll have to use the "normal" looping syntax.

for (int i = 0; i < array1.length; i++)
{
String s1 = array1;
String s2 = array2.
....
}

-- chris
 
J

John Ersatznom

gaurav said:
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.....

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, array2);
}

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!
 
D

Daniel Pitts

gaurav said:
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

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;
Object bValue = b;
}
---
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.
 
G

gaurav v bagga

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
 
G

gaurav v bagga

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
 
A

Andreas Leitgeb

gaurav v bagga said:
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"

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.
 
G

gaurav v bagga

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
 
A

Andreas Leitgeb

gaurav v bagga said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top