Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!


char firstLetter = userFavoriteLetters[ 0 ]; The number provided in those square brackets is called the index



Download 3,91 Mb.
Pdf ko'rish
bet29/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   25   26   27   28   29   30   31   32   ...   70
Bog'liq
Java for Fucking Idiots

char
firstLetter = userFavoriteLetters[
0
];
The number provided in those square brackets is called the index .
In any array where you want to put things in or take things out, you specify
the index at which you’re doing your shit.
Let’s check your knowledge here.
If my 
char
array has 10 slots in it, how do we get the last one in the array?
What “index” do we put in the square brackets? Think about it before
reading on. Open a window if you need to.
The answer:
userFavoriteLetters[
10
]
…would be absolutely INCORRECT .
You actually need to put a 
9
there to get the last element in the box!! Why?
Because arrays start at 0! And if there’s 10 slots, then the last character is at
index 9! One before the size of the array.


10 – 1 = 9 .
userFavoriteLetters[
9
]
Returns the last item in the array.
This is a concept that your mind might have a hard time adjusting to. Well,
pretty much any new thought or idea is probably a bit puzzling to you. But
this one especially.
Just remember, when dealing with arrays you need to mentally subtract 1
from how you’d say it in English.
The item in the 1
st
position is at index 
0
.
The item in the 2
nd
position is at index 
1
.
The item in the 5
th
position is at index 
4
.

BRAIN TEASER TIME
What happens if you ask the array for an item in a slot that doesn’t exist?
For example, in our 10-slot array, what if we asked for the user’s 15
h
favorite letter?
char
fifteenthFavoriteLetter = userFavoriteLetters[
14
];
The answer is your program “crashes.” An “exception” occurs. Everything
goes to absolute shit.
Ever see a program on your computer or phone or game console just
completely close and tell you to press a button to report the error?
There are many ways to prevent this. You could only look inside the box
once you’ve made sure that the box is at least that big (using an 
if
check).
Or do something fancy called “
exception handling
” which I won’t be
getting to in this book.
Lists


Java has some extremely helpful classes that we can use in place of arrays.
While I won’t be covering them in this book, feel free to 
take a gander
.


CHAPTER 8: LOOPS
“You make a loop de loop and pull
And your shoes are looking cool
You go over and back, left to right
Loop de loop and you pull them tight”
– Spongebob Squarepants
Welcome to the section on loops.
Java has 3 kinds of loops. One of them is used all the time. Another is used
every once in a while. The other is almost never used. I’ll let you guess
them as we go.
You: Hold, on the hell is a loop?
Life. Life is a loop.
You wake up. You get ready for the day. You go to school or work. You
come home exhausted. You try to relax if you have time. You sleep. Repeat.
We can do the same thing, slightly less depressingly, in Java code!
Let’s back up to when I introduced arrays with the alphabet.

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   25   26   27   28   29   30   31   32   ...   70




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