How this feels like an exercise, there's only one idea for you. I believe that the algorithm you are looking for, using only arrays, is similar to:
[While Array1 has values) Get a x value of Array1 (you did it in the first for >)
[While Array2 has values] They get a value of Array2 (another for similar to the first) by comparing it with < strong> x and ...
- A) If it is the same, add the value in the ArrayResult (your Array res).
- B) If not, well, do nothing.
At the end of this computation, you should get the result you are looking for (which should be "Peter" and "Ana").
X : Pedro
X : Diego
X : Ana
X : Carlos
RES: Pedro
RES: Ana
One detail: Be careful with your Array declaration result String[] res = {}
. When you declare an array in this way, you are effectively creating an array with zero positions, which will cause you problems when trying to assign a value.
My suggestion for you is to declare it this way:
String res[] = new String[nomes.length];
Doing this, even though all names in the Array names are in the Comparison Array, you will have no problems.
Issue
Initially, I thought it would be better not to talk about it but, on second thought, I think it's useful to leave a comment here about the "copy" of arrays (as quoted in the question), for completeness.
When we assign an Array to the other in Java, its values are not copied - which happens to be a copy of the Array's reference . After this operation, both variables effectively represent the same Array.
For more information, this entry of Stackoverflow EN, can be useful (if there is a in PT that someone knows, edit). An example commented:
public static void passagemDeReferencia() {
String[] array1 = {"Pedro", "Diego", "Ana", "Carlos"};
String[] array2 = array1; // Isto passa uma referência do Array - não é uma cópia
// Ambos arrays são idênticos, conforme visto comparando a primeira posição.
System.out.println("array1[0] == array2[0]: " + array1[0].equals(array2[0]));
System.out.println("array1[0]: " + array1[0]);
System.out.println("array2[0]: " + array2[0]);
// array1[0] == array2[0]: true
// array1[0]: Pedro
// array2[0]: Pedro
// Alterar qualquer posição do array2, irá alterar o array1.
array2[0] = "Miguel";
System.out.println("array1[0] == array2[0]: " + array1[0].equals(array2[0]));
System.out.println("array1[0]: " + array1[0]);
System.out.println("array2[0]: " + array2[0]);
// array1[0] == array2[0]: true
// array1[0]: Miguel
// array2[0]: Miguel
// Assim como alterar qualquer posição de array1, irá alterar o array2.
array1[0] = "Pedro";
System.out.println("array1[0] == array2[0]: " + array1[0].equals(array2[0]));
System.out.println("array1[0]: " + array1[0]);
System.out.println("array2[0]: " + array2[0]);
// array1[0] == array2[0]: true
// array1[0]: Pedro
// array2[0]: Pedro
}
For copy needs, you should use an Array Copy.
public static void copiandoArrays() {
String[] array1 = {"Pedro", "Diego", "Ana", "Carlos"};
String[] array2 = new String[array1.length];
System.arraycopy(array1, 0, array2, 0, array1.length); // Este método efetua uma cõpia do array
// Mais código
}