[英]How do I write a generic method to swap values of items in an array?
我試圖讓這段代碼工作。 大多數代碼都是作為問題提供的,但我只是不斷地收到一個又一個錯誤,我根本不明白。
package generic.collections;
import java.util.ArrayList;
public static void <T> T swap(Collection<T>, T x, T y) {
T temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
您的定義已關閉,首先void
方法不能返回T
。 數組不是Collection
。 而x
和y
應該是int
(s)。 喜歡,
public static <T> void swap(T[] a, int x, int y) {
T temp = a[x];
a[x] = a[y];
a[y] = temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.