Java how to add these elements to int array using loop then return the array -
i can't seem figure out how i'm not getting right output
int [] f = {1,2,3,4,5,5,4,3,2,1}; int [] b = {0,3,3}; system.out.println(arrays.tostring(hide(f,b))); // declare int array returned following method int [] hiddenat = {}; public static int [] hide(int [] front, int [] back) { if (back.length > front.length) { system.exit(0); } for(int x = 0; x < (front.length - 1); x++){ for(int y = 0; y < (back.length - 1); y++){ int temp = front[y]; if (front[y] - back[y] <= front[y] && front[y] - back[y] >= 0 ) { hiddenat.add(temp); } } } return hiddenat; } i'm trying compare first , second array find second array fit first array without going on max on first so...
first time through loops: 0 compares 1, 2 compares 3, 3 three (this doesn't work) second time shifts positions: 0 2, 2 3, 3 4 (works) third time: 0 3, 3 4, 3 5 (works again)
then every position in array works array return values in example front[1], front[2], front[3] on such returned array looks {1, 2, 3 ... }
your loop needs this:
for(int x = 0; x < (front.length); x++){ for(int y = 0; y < (back.length); y++){ int temp = front[y]; if (front[x] - back[y] <= front[x] && front[x] - back[y] >= 0 ) { hiddenat.add(temp); } } } also need take out -1 length, example length of front 10. array goes 0 - 9 starting @ 0, 10 different values -1 took 0 - 8 , lost values. can keep -1 if <= instead of <.
write on piece of paper , point each spot in arrays step through , literally see happening if doesn't result.
Comments
Post a Comment