looping through a 2D array 3 items at a time (Java) -
i having trouble trying figure out way move through 2d array 3 items @ time because has done in specific way.
the array has individuals rows , loci columns. have take 2 individuals @ time , compare them every third until end:
so if there 5 individuals total individual comparisons 0,1,2, 0,1,3, 0,1,4, 1,2,3, 1,2,4 , 2,3,4
for each of these comparisons, have compare alleles @ each locus (each column represents allele, 2 columns 1 locus). looks
locus1 locus2 locus3 ... 0 1 2 3 4 5 0 1 2 3 4 5 6 1 7 8 9 10 11 12 2 13 14 15 16 17 18 3 19 20 21 22 23 24 4 25 26 27 28 29 30 definite example of mean, have first set: 0,1,2. first comparison of alleles: 1,2, 7,8 , 13, 14. next comparison of alleles: 3,4, 9,10 , 15,16. last (assuming 3 loci) be: 5,6, 11,12, , 17,18.
for every set of 6 alleles, have comparison. have finished code comparison testing. im not sure how loop through 2d array. thinking psuedo-code this:
each pair of individuals (i,j) each locus l each individual k, not or j if comparison test works great else better luck next time
i not want direct answer. guidance how tackle keeping array boundaries in mind. have not done search before.
i appreciate help! :)
there 2 ways can think of. these days, have classic external iterations, , more modern (but limited) internal stream iterations. i'll show both.
i'm assuming collection/array of type allele
, called alleles
, that's entirely arbitrary.
for external iteration, this:
for(allele : alleles) { for(allele j : alleles) { if(i == j) continue; for(allele k : alleles) { if(i == j || == k) continue; docomparison(i, j, k); } } }
basically, test equivalencies before entering next loop, , if find one, skip next entry.
for internal iteration (functional/streams), this
alleles.stream().foreach(i -> alleles.stream().filter(j -> != j).foreach(j -> alleles.stream().filter(k -> k != && k != j).foreach(k -> docomparison(i, j, k) ) ) );
in instance, may bit more efficient, filter(…)
methods remove each potential conflict before beginning iteration. if you're dealing more three, though, may have clever iterations.
hopefully it. luck!
Comments
Post a Comment