Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using advanced for loop instead of for loop

I am a bit confused and I would need some clarification. Not too sure if I'm on the right track, hence this thread.

Here is my code that I want to decipher into advanced foreach loop.

    int[] arrayA = {3, 35, 2, 1, 45, 92, 83, 114};
    int[] arrayB = {4, 83, 5, 9, 114, 3, 7, 1};
    int n = arrayA.length;
    int m = arrayB.length;
    int[] arrayC = new int[n + m];
    int k = 0;

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(arrayB[j] == arrayA[i])
            {
                arrayC[k++] = arrayA[i];
            }
        }
    }
    for(int i=0; i<l;i++)
        System.out.print(arrayC[i] + " ");
    System.out.println();

So far this is the point where I am stuck at:

    int[] a = {3, 8, 2, 4, 5, 1, 6};
    int[] b = {4, 7, 9, 8, 2};
    int[] c = new int[a.length + b.length];
    int k = 0;
    for(int i : a)
    {
        for(int j : b)
        {
            if(a[i] == b[j]) 
            {
                c[k++] = a[i];
            }
        }
        //System.out.println(c[i]);
    }
    for(int i=0; i<c.length;i++)
        System.out.print(c[i] + " ");
    System.out.println();
}
like image 905
winnergo Avatar asked May 07 '26 10:05

winnergo


2 Answers

You are almost there

for(int i : a)
{
    for(int j : b)
    {
        if(i == j) 
        {
            c[k++] = i;
        }
    }
}

With for(int i : a) access the elements in the array a using i. If a is {3, 8, 2, 4, 5, 1, 6}, then i would be 3,8,2,.. on each iteration and you shouldn't use that to index into the original array. If you do, you would get either a wrong number or a ArrayIndexOutOfBoundsException


Since you want to pick the numbers that are present in both the arrays, the length of array c can be max(a.length, b.length). So, int[] c = new int[Math.max(a.length, b.length)]; will suffice.

If you want to truncate the 0s at the end, you can do

c = Arrays.copyOf(c, k);

This will return a new array containing only the first k elements of c.

like image 52
user7 Avatar answered May 09 '26 00:05

user7


I would use a List and retainAll. And in Java 8+ you can make an int[] into a List<Integer> with something like,

int[] arrayA = { 3, 35, 2, 1, 45, 92, 83, 114 };
int[] arrayB = { 4, 83, 5, 9, 114, 3, 7, 1 };
List<Integer> al = Arrays.stream(arrayA).boxed().collect(Collectors.toList());
al.retainAll(Arrays.stream(arrayB).boxed().collect(Collectors.toList()));
System.out.println(al.stream().map(String::valueOf).collect(Collectors.joining(" ")));

Outputs

3 1 83 114

Alternatively, if you don't actually need the values besides displaying them, and you want to use the for-each loop (and less efficiently) like

int[] arrayA = { 3, 35, 2, 1, 45, 92, 83, 114 };
int[] arrayB = { 4, 83, 5, 9, 114, 3, 7, 1 };
for (int i : arrayA) {
    for (int j : arrayB) {
        if (i == j) {
            System.out.print(i + " ");
        }
    }
}
System.out.println();
like image 27
Elliott Frisch Avatar answered May 09 '26 01:05

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!