fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void interseccao(int[] vetorA, int[] vetorB){
  11. int indiceA = 0;
  12. int indiceB = 0;
  13.  
  14. while (indiceA < vetorA.length && indiceB < vetorB.length){
  15. if (vetorA[indiceA] == vetorB[indiceB]){
  16. System.out.print(vetorA[indiceA] + " ");
  17. indiceA++;
  18. indiceB++;
  19. }
  20. else if (vetorA[indiceA] < vetorB[indiceB]){
  21. indiceA++;
  22. }
  23. else {
  24. indiceB++;
  25. }
  26. }
  27.  
  28. }
  29.  
  30. public static void main(String[] args){
  31.  
  32. int[] vetorA = {1,2,3,4,5,6,7,8};
  33. int[] vetorB = {3,4,7,8,9,10};
  34.  
  35. interseccao(vetorA, vetorB);
  36. }
  37. }
Success #stdin #stdout 0.05s 27692KB
stdin
Standard input is empty
stdout
3 4 7 8