ジェネリックスまでJavaでうまくいきました。私はすべてのデータ型を受け入れるジェネリックを使用するためにこのキューを変換するのに苦労しています。Javaのジェネリックを使用してint型をint型に変換できません
item
はitem
は、「任意の」データ型を表す必要がある場合int
に変換するカント私はなぜ理解していません。
また、削除関数も作成する必要があるので、ジェネリックスについての説明と少し説明すると便利です。
private int initialCapacity = 10;
private int size = 0;
private int[] content;
public static <E> void add(E item) {
int size = 0;
int[] content;
// If we run out of space in the array
if (size == content.length) {
// 1) Create a new, larger array (2x)
int[] newArray = new int[content.length * 2];
// 2) Copy the content from the old array to the new one
for (int c = 0; c < content.length; c++) {
newArray[c] = content[c];
}
// 3) Let the old array point to the new array
content = newArray;
}
//Add the item to the content of the array list
content[size] = item;
size++;
}
なぜそれがintであることが分かっているのですか? –
まず、プリミティブ*ではジェネリックスは機能しません。第二に、「E」は制約されていないので、何でもよい。 'String'、' int = String'は無効です。 – Andreas