java - print first array element when it ends -
as topic states , previous post
which requires me print 4 consecutive numbers in array locations
i have managed understand code given me. fast snail comment made me thought program thoroughly , indeed got me thinking on how make array print it's first location's element again.
as e.g (" note " array size 20) array location given me 18, program have print elements in location 19, 20, 1 ,2 . have managed understand on how print elements in location 19 , 20 have no idea how program print it's first position.
thanks once again in advanced given!
use mod operation length of array.
18 % 20 = 18
19 % 20 = 19
20 % 20 = 0 (first element of array)
21 % 20 = 1
int firstprint = 18;//or random value for(int = firstprint; < firstprint + 4; i++) { system.out.println(myarray[firstprint % myarray.length]); }
Comments
Post a Comment