Since this looks like an exercise, there’s only one idea for you. I believe that the algorithm you seek, using only arrays, is similar to:
- [As the Array1 hold values] Get a value x of Array1 (you did it in the first for)
- [As the Array2 have values] Get a value of Array2 (another for similar to the first) comparing it with the value x and...
- A) If equal, add the value in the Array (your Array res).
- B) If not, well, do nothing.
At the end of this computation, you should get the search result (which should be "Pedro" and "Ana").
X : Pedro
X : Diego
X : Ana
X : Carlos
RES: Pedro
RES: Ana
One detail: take care with your result Array declaration String[] res = {}
. When you declare an array this way, you are effectively creating an array with zero positions, which will cause you problems when trying to assign a value.
My suggestion to you is to declare this way:
String res[] = new String[nomes.length];
By doing this, even if all the names of the Array names are in the Comparison Array, you will have no problems.
Editing
Initially, I thought it would be better not to talk about it but, on second thought, I find it useful to leave here a comment regarding the "copy" of arrays (as mentioned in the question), for completeness.
When we assign an Array to another, in Java, its values are not copied - what happens is a copy of reference of the Array. After this operation, both variables effectively represent the same Array.
For more information, this Stackoverflow EN input, may be useful (if there is one in PT that someone knows, edit). A commented example:
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 copying needs, 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
}
I apologize for the editing errors and thank you very much for the amendments Denis Rudnei de Souza.
– Alan.O.S
Thank you. About the editions that I made
– Denis Rudnei de Souza
Obs: If you wish to notify someone, you can put a
@
in front of the name, so@Denis Rudnei de Souza
– Denis Rudnei de Souza