Intersection() method

I have the same question as Jack007 at page:

There was a solution given but I don’t understand. Can someone explain it to me?

This was part of the exercizes in the Arrays module lesson 10:

Lesson-question:

Extend the Array class and add a method to return the common items in this array and another array.

My question:

How do I use the intersection() method in the Array.

I have attached two screenshots to show my code.

Best,

Raimon


You need to save the Array returned by the intersect method into a variable and then print that. Also, the intersect operation is really only useful with two different arrays.

For example:

// ... setup numbers with 1..7
Array otherNumbers = new Array(3);
otherNumbers.insert(1);
otherNumbers.insert(5);
otherNumbers.insert(10);
Array intersection = numbers.intersect(otherNumbers);
intersection.print(); // should have 1 and 5

Thank a lot jmrunkle, I’m going to try your explanation later. Just wanted to respond to show my appreciation.

1 Like

Thanks again Jason, it worked :+1:

1 Like