剛剛開始自學代碼,遇到了一點問題,大家幫我看一下為什么程式執行的結果會是這樣的?
下面代碼中,我在Sort類中定義了一個turn()的方法,想完成陣列的轉置,讓陣列中的元素前后顛倒。
但是當我呼叫了這個方法以后,輸出的確實原來陣列的內容,并沒有發生前后顛倒。請問究竟為什么會導致這樣的結果?
代碼如下:
class Sort
{
public static void printArray(int [] temp){ //對陣列進行列印的方法
for(int x : temp){
System.out.print(x + "\t");
}
System.out.println();
}
public static void turn(int [] temp){ //陣列轉置的方法
int [] t = new int [(temp.length)];
int foot = temp.length -1;
for(int x = 0 ; x < temp.length ; x++){
t[foot --] = temp[x];
}
temp = t;
}
}
public class ArrayDemo
{
public static void main(String args[]){
int data [] = new int [] {8,9,0,2,3,5,10,7,6,1};
Sort.turn(data);
Sort.printArray(data);//輸出:8,9,0,2,3,5,10,7,6,1
}
}
uj5u.com熱心網友回復:
注意變數作用域
class Sort
{
public static void printArray(int [] temp){ //對陣列進行列印的方法
for(int x : temp){
System.out.print(x + "\t");
}
System.out.println();
}
public static int[] turn(int [] temp){ //陣列轉置的方法
int [] t = new int [(temp.length)];
int foot = temp.length -1;
for(int x = 0 ; x < temp.length ; x++){
t[foot --] = temp[x];
}
return t;
}
}
public class ArrayDemo
{
public static void main(String args[]){
int data[]= new int [] {8,9,0,2,3,5,10,7,6,1};
data=https://bbs.csdn.net/topics/Sort.turn(data);
Sort.printArray(data);
}
}
uj5u.com熱心網友回復:
你只是把 值單方面的賦值到后面值并沒有交換 你可以這樣
public static void turn(int [] temp){ //陣列轉置的方法
int [] t = new int [temp.length];
int tempx;
for(int x = 0 ; x < temp.length/2 ; x++){
tempx=temp[x];
temp[x]=temp[temp.length-x-1];
temp[temp.length-x-1]=tempx;
}
temp = t;
}
uj5u.com熱心網友回復:
非常感謝前輩的回復,提供回傳值的方法確實能夠解決這個問題,而且簡單明了。
不過,關于“變數作用域”,我其實還是不明白為什么會錯。我之所以在這個問題上這么糾結,是因為陣列是參考資料型別,在我的代碼中,通過參考傳遞,temp [ ]→data[ ] ,之后在turn( )中通過一系列操作,將data[ ]中的元素前后順序顛倒賦值給新的陣列t[ ] ,然后把賦值之后的陣列t[ ]重新賦值還給data[ ],這個程序為什么會涉及到變數作用域的問題?
我在我的代碼中重新增加了測驗變數數值,請看下面的代碼:
class Sort
{
public static void printArray(int [] temp){ //對陣列進行列印的方法
for(int x : temp){
System.out.print(x + "\t");
}
System.out.println();
}
public static void turn(int [] temp){ //陣列轉置的方法
int [] t = new int [(temp.length)];
int foot = temp.length -1;
for(int x = 0 ; x < temp.length ; x++){
t[foot --] = temp[x];
}
printArray(t); //輸出:1 6 7 10 5 3 2 0 9 8
temp = t;
printArray(temp); //輸出:1 6 7 10 5 3 2 0 9 8
}
}
public class ArrayDemo
{
public static void main(String args[]){
int data [] = new int [] {8,9,0,2,3,5,10,7,6,1};
Sort.turn(data);
Sort.printArray(data); //輸出:8 9 0 2 3 5 10 7 6 1
}
}
當然,我也知道,解決這個陣列轉置問題可以通過修改代碼有多種實作方式和思路,我只是想弄明白程式輸出意想不到的結果的原因究竟是什么?是不是我對某個哪個知識點的理解本身就有問題,希望前輩不吝賜教。
uj5u.com熱心網友回復:
當然,我也知道,解決這個陣列轉置問題可以通過修改代碼有多種實作方式和思路,我只是想弄明白程式輸出意想不到的結果的原因究竟是什么?是不是我對某個哪個知識點的理解本身就有問題,希望前輩不吝賜教。
因為陣列是作為引數傳入的,方法獲得的是引數的地址的參考。當temp=t,temp的地址參考被改為t的地址參考,不會對原來的地址有影響,只有改變引數參考地址內的值才會對元陣列產生影響。
#2的方法 和最后有沒有temp=t都沒關系,因為在改變temp地址參考前已經改變了參考的引數地址內的值。
uj5u.com熱心網友回復:
這個是實參和形參的問題,你可以百度看一下,形參改變不會影響實參的uj5u.com熱心網友回復:
剛剛開始自學代碼,遇到了一點問題,大家幫我看一下為什么程式執行的結果會是這樣的?
下面代碼中,我在Sort類中定義了一個turn()的方法,想完成陣列的轉置,讓陣列中的元素前后顛倒。
但是當我呼叫了這個方法以后,輸出的確實原來陣列的內容,并沒有發生前后顛倒。請問究竟為什么會導致這樣的結果?
代碼如下:
class Sort
{
public static void printArray(int [] temp){ //對陣列進行列印的方法
for(int x : temp){
System.out.print(x + "\t");
}
System.out.println();
}
public static void turn(int [] temp){ //陣列轉置的方法
int [] t = new int [(temp.length)];
int foot = temp.length -1;
for(int x = 0 ; x < temp.length ; x++){
t[foot --] = temp[x];
}
temp = t;
}
}
public class ArrayDemo
{
public static void main(String args[]){
int data [] = new int [] {8,9,0,2,3,5,10,7,6,1};
Sort.turn(data);
Sort.printArray(data);//輸出:8,9,0,2,3,5,10,7,6,1
}
}
參考你一樓的方法來說明.
turn()方法執行到行號為12的代碼時,java虛擬機記憶體資料區簡略資訊入下圖

此時turn()方法中的變數temp與main()方法中的變數data都指向同一個陣列{8,9,0,2,3,5,10,7,6,1},而turn()方法中的變數t指向空陣列
執行turn()方法到行號17行時,java虛擬機記憶體資料區簡略資訊入下圖

此時turn()方法中的變數temp與變數t都指向同一個陣列{1,6,7,10,5,3,2,0,9,8},而main方法中的變數data仍然指向陣列{8,9,0,2,3,5,10,7,6,1}.
執行完turn()方法,turn()方法堆疊幀彈出虛擬機堆疊記憶體,變數temp與變數t不復存在,沒有變數指向堆記憶體中的陣列{1,6,7,10,5,3,2,0,9,8},這個陣列在合適的時機會被虛擬機回收掉.
回到main方法,執行printArray()方法,行號為4,此時的java虛擬機記憶體資料區簡略資訊入下圖

printArray()方法的變數temp指向陣列{8,9,0,2,3,5,10,7,6,1},剩下的就是回圈列印陣列中的元素.
希望對你有幫助.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/64693.html
標籤:Java SE
