我在Java的初學者,我需要一個數組的內容複製到另一個變量。但是,Java總是通過引用而不是按值傳遞數組。
這裏就是我的意思是,如果這是令人困惑:
int test[]={1,2,3,4};
int test2[];
test2=test;
test2[2]=8;
for(int i=0;i<test2.length;i++)
System.out.print(test[i]); // Prints 1284 instead of 1234
在這個例子中,我不希望的test
值改變。這可能沒有使用任何Java的更高級功能,如ArrayList和Vectors?
編輯:我試過System.ArrayCopy和test.clone(),但他們似乎仍然沒有工作。 這裏是我的實際代碼:
temp_image=image.clone();
for(int a=0;a<image.length;a++)
for(int b=0;b<image[0].length;b++)
image[a][b]=temp_image[image.length-1-a][b];
基本上我試圖翻轉「圖像」倒掛。代碼中有沒有錯誤?
System.arraycopy:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – 2012-04-13 02:34:12
@LuiggiMendoza你可能要開始使用更多最近的JavaDocs; [Java 1.4是3個版本(10年)](http://en.wikipedia.org/wiki/Java_version_history)。 – 2012-04-13 02:37:08
當然,這有2000個確切的重複嗎? – 2012-04-13 02:43:17