我想寫一個方法,它複製了我的PaperPublication陣列:寫作是需要對象的數組的方法,複製它,然後返回它
public static PaperPublication[] copyBooks(PaperPublication[] array)
{
PaperPublication[] copy = new PaperPublication[array.length];
for (int i = 0; i < copy.length; i++) {
PaperPublication temp = array[i];
if (temp != null) {
copy[i] = new PaperPublication(temp);
}
}
return copy;
}
當我嘗試並打印出複製的數組像這樣,
PaperPublication[] copy = driverClass.copyBooks(original);
for (int i = 0; i < 12; i++)
{
System.out.println(copy.toString());
}
它產生的位置,而不是值:
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
[LPackage1.PaperPublication;@15db9742
我怎樣寫代碼,以便它重新改變價值觀? (我的類中已經有一個覆蓋的toString()方法)
您在打印時忘記了指定索引:'System.out.println(copy [i]);' – janos