Effective Java


Object pop() { if (size == 0) throw new EmptyStackException(); Object



Download 2,19 Mb.
Pdf ko'rish
bet120/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   116   117   118   119   120   121   122   123   ...   341
Bog'liq
Effective Java

Object
pop() {
if (size == 0)
throw new EmptyStackException();
Object
result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
public boolean isEmpty() {
return size == 0;
}
private void ensureCapacity() {
if (elements.length == size)
elements = Arrays.copyOf(elements, 2 * size + 1);
}
}
This class should have been parameterized to begin with, but since it wasn’t, we
can 
generify
it after the fact. In other words, we can parameterize it without harm-
ing clients of the original non-parameterized version. As it stands, the client has to
cast objects that are popped off the stack, and those casts might fail at runtime.
The first step in generifying a class is to add one or more type parameters to its


ITEM 29: FAVOR GENERIC TYPES
131
declaration. In this case there is one type parameter, representing the element type
of the stack, and the conventional name for this type parameter is 
E
(Item 68).
The next step is to replace all the uses of the type 
Object
with the appropriate
type parameter and then try to compile the resulting program:
// Initial attempt to generify Stack - won't compile!
public class Stack

{
private 
E
[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
elements = new 
E
[DEFAULT_INITIAL_CAPACITY];
}
public void push(
E
e) {
ensureCapacity();
elements[size++] = e;
}
public 
E
pop() {
if (size == 0)
throw new EmptyStackException();
E
result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
}
... // no changes in isEmpty or ensureCapacity
}
You’ll generally get at least one error or warning, and this class is no exception.
Luckily, this class generates only one error:
Stack.java:8: generic array creation
elements = 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   116   117   118   119   120   121   122   123   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish