arrays - Writing a continous 'for' loop in java -


i'm new java , studying on for loops online tutorials , question stuck mind. explain doubt using example.

lets there int array of size 4 elements {1, 2, 3, 4}. suppose user wants print elements of array in way :

{3,4,1,2,3,4,1,2,3,4} 

the user wants print array third number till end of array , if array ends, array should start again first , goes on until total numbers printed should 10. can possible ? or there way can achieve ?

can loop printed again first after ends? tried thinking of using list not able come answer. kindly me giving suggestions. thanks

you use modulo % operator loop through array multiple times. code below prints numbers in format described.

int[] array = {1, 2, 3, 4}; int start = 2; // 0 indexed position of 3rd number int numtimestoprint = 10;  system.out.print("{"); (int = 0; < numtimestoprint; i++) {     if (i > 0) system.out.print(",");     system.out.print(array[(i + start) % array.length]); } system.out.print("}"); 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -