The idea would be if the index sought is greater than the size of the array, make the difference: n=n-array.length;
until it is no longer greater.
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int [] a={1,2,3,4};
secuenceGetID(a,6);
}
public static int secuenceGetID(int[] array, int n){
while(n>=array.length){
n=n-array.length;
}
System.out.println(array[n]);
return array[n];
}
}
now I present another solution, by congruence of numbers , that is:
26 es congruente a 11 con modulo 5
26%5==1 y 11%5==1
according to this analogy, it would only be enough with: n=n%array.length
;
ejemplo: * n=5 es el indice a buscar
* mi array es {1,2,3,4}, con tam de 3
entonces 5%3=2 el resultado sera 3
the serious code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int [] a={1,2,3,4};
secuenceGetID(a,6);
}
public static int secuenceGetID(int[] array, int n){
n=n%array.length;
System.out.println(array[n]);
return array[n];
}
}