Effective Java


// EnumSet - a modern replacement for bit fields



Download 2,19 Mb.
Pdf ko'rish
bet155/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   151   152   153   154   155   156   157   158   ...   341
Bog'liq
Effective Java

// EnumSet - a modern replacement for bit fields
public class Text {
public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }
// Any Set could be passed in, but EnumSet is clearly best
public void applyStyles(Set

ITEM 37: USE ENUMMAP INSTEAD OF ORDINAL INDEXING
171
Item 37: Use 
EnumMap
 instead of ordinal indexing
Occasionally you may see code that uses the 
ordinal
method (Item 35) to index
into an array or list. For example, consider this simplistic class meant to represent
a plant:
class Plant {
enum LifeCycle { ANNUAL, PERENNIAL, BIENNIAL }
final String name;
final LifeCycle lifeCycle;
Plant(String name, LifeCycle lifeCycle) {
this.name = name;
this.lifeCycle = lifeCycle;
}
@Override public String toString() {
return name;
}
}
Now suppose you have an array of plants representing a garden, and you want
to list these plants organized by life cycle (annual, perennial, or biennial). To do
this, you construct three sets, one for each life cycle, and iterate through the
garden, placing each plant in the appropriate set. Some programmers would do
this by putting the sets into an array indexed by the life cycle’s ordinal:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   151   152   153   154   155   156   157   158   ...   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