How to traverse a vector with the quantity of the second vector?

Asked

Viewed 112 times

2

int[] teste1 = {3524, 79, 2573, -4216, -4126, -4169, 1876, 2903, 2702, 1090, -6544, -8600};

int[] teste2 = {3524, 79, 2573, -4216, -4126, -4169};

I have a vector with 12 records and another with 6, I need to go twice without repeating.

Example, first process of 3524 until 4169 afterward 1876 until -8600.

I’m doing this to compare the second with the first.

  • 2

    for(int i = 0; i < teste1.length/2; i++) {} or for(int i = 0; i < 6; i++) {}. Your doubt is that same?

  • This way it will go through 6 records and stop. I need it to run twice, first from 0 to 5 and then from 6 to 11.

1 answer

2


Do so:

int[] teste1 = {3524, 79, 2573, -4216, -4126, -4169, 1876, 2903, 2702, 1090, -6544, -8600};

    int[] teste2 = {3524, 79, 2573, -4216, -4126, -4169};

    for(int t1=0; t1<teste1.length; t1++){
        int t2 = t1%6;    
        System.out.println("Comparando teste1:"+ teste1[t1] + "com teste2:"+teste2[t2]);

    }

The mod of the two vectors will return exactly what you want! Follow the output:

Comparando teste1:3524 com teste2:3524
Comparando teste1:79 com teste2:79
Comparando teste1:2573 com teste2:2573
Comparando teste1:-4216 com teste2:-4216
Comparando teste1:-4126 com teste2:-4126
Comparando teste1:-4169 com teste2:-4169
Comparando teste1:1876 com teste2:3524
Comparando teste1:2903 com teste2:79
Comparando teste1:2702 com teste2:2573
Comparando teste1:1090 com teste2:-4216
Comparando teste1:-6544 com teste2:-4126
Comparando teste1:-8600 com teste2:-4169

Browser other questions tagged

You are not signed in. Login or sign up in order to post.