Why does the value of Numbers[0]
change to 3 after executing the line of code 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);
}