public class Main {
public static void main(String[] args) {
int[] a ={5,6};
System.out.println("Array indexes values before calling the method ");
for ( int element: a ){
System.out.println(element);
}
Set(a);
System.out.println("Array indexes values after calling the method ");
for ( int element: a ){
System.out.println(element);
}
int l= 5;
System.out.println("Variable value before calling the method ");
System.out.println(l);
Set(l);
System.out.println("Variable value after calling the method ");
System.out.println(l);
}
static void Set(int[] k){
k[1] =8;
k[0] =2;
}
static void Set(int p){
p =3;
}
}
Die Ausgabe dieses Codes ist:
Array indexes values before calling the method
5
6
Array indexes values after calling the method
2
8
Variable value before calling the method
5
Variable value after calling the method
5
Process finished with exit code 0
Wenn ich also das Array als Argument eingebe und den Parameter-Array-Indexwert ändere, ändert sich der Wert des Index im Array, der als Argument übergeben wurde, aber das passiert nicht, wenn die Parameter ist variabel? und wie kann man verhindern, dass die Methode „Set“ die Array-Werte ändert?
Warum ändert die Methode Array-Indizeswerte, aber nicht den Variablenwert in Java? [Duplikat] ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post