6 Views0 Thanks

How to loop through array items multiple times

Testing Forums Testing Forums Programing Language Java How to loop through array items multiple times

Viewing 2 posts - 1 through 2 (of 2 total)
  • #243

    Anonymous
    Inactive

    Can some one suggest a method to loop through an array multiple times. Let us say if we have an array of length 5, I have to loop the items of array 10 times.

    String[] a = new String[]{“a”,”b”,”c”,”d”,”e”};

    I need to print
    abcdeabcde

    How can we do that?

    #244

    Shashi Kumar
    Participant

    We can use the % Modulus operator to which returns the remainder. In the below code we are taking two variable one is i and j. i and j are looped 10 times. The value of i goes from 0-5 for 2 iterations which is because of Modulus %;

    String[] a = new String[]{"a","b","c","d","e"};
    for(int i =0,j=0 ; i< a.length*2 && j < a.length*2 ; i++, j++) {
    			i = i%a.length;
    			System.out.println(a[i]);
    		}
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.