0
Why the value of Numbers[0]
changes to 3 after executing the code line below?
New_Numbers [0] = Numbers [Numbers.length - 1];
The complete code:
public static void main(String[] args)
{
System.out.print("Indique o número de elementos do array: ");
int a = scanner.nextInt();
int [] Numbers = new int [a];
System.out.println("Indique os três elementos do array:");
for ( int i = 0; i < a; i++ )
{
System.out.print("Número na posição " + i + " do array: ");
Numbers [i] = scanner.nextInt();
}
int [] New_Numbers = Numbers;
New_Numbers [0] = Numbers [Numbers.length - 1];
New_Numbers [New_Numbers.length - 1] = Numbers [0];
String Numbers_S = Arrays.toString(Numbers);
String New_Numbers_S = Arrays.toString(New_Numbers);
System.out.println("Array original: " + Numbers_S);
System.out.println("Novo array após a troca entre o primeiro e o último elementos: " + New_Numbers_S);
}
I get it. Thank you.
– Diogo Pereira
Diogo, the best way to thank for an answer is to mark it as accepted by clicking on the visa sign ( ). But do this only if any answer has answered your original question. When you reach 15 reputation points you can also vote in favour of an answer or question. See: Someone answered me and Why vote?. Also do the tour, for an initial explanation of how the site works.
– Pedro Gaspar