Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

java - Find all possible pairs in an array

Its when I try to do stuff like this I realise I really need to go to university!

Anyway I have an array of strings (275) I need to loop through them and create strings of all the possible pairs, in Java.

I've been learning about recursion but I cant find the answer for this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In case pairs ab and ba are different, do:

for i=0 to array.length
  for j=0 to array.length
    if i == j skip
    else construct pair array[i], array[j] 

and if not, do something like this:

for i=0 to array.length-1
  for j=i+1 to array.length
    construct pair array[i], array[j] 

Note that I am assuming the array holds unique strings!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...