Sužinokite apie „Java“ programavimą: kaip naudoti sąrašus, temas ir kt.
Paskutiniame mūsų „Java“ serijos straipsnyje paaiškiname, kaip tinkamai tvarkyti „Java“ gijas, sąrašus, išimtis ir failus.
„Java“: kokios yra išimtys?
Jei dar nesate dirbę su „Java“, pirmiausia galite perskaityti mūsų „Pagrindai“. Įvairius pavyzdžių kodus galite rasti paveikslėlių galerijoje, esančioje straipsnio pabaigoje.
- Išimtis yra problema, iškylanti programos vykdymo metu.
- Yra specialus kodas, kaip elgtis su tokia komanda. Norėdami tai padaryti, pirmiausia parašykite „pabandykite {“ ir tada kodą, kurį reikia vykdyti. Tada parašykite „} pagauk (išimtis e) {“ ir tada nurodyk įvairias komandas, kurias reikia vykdyti, jei ankstesnė operacija neįmanoma. Nepamirškite pabaigoje pridėti petnešą.
- Mesti komandą taip pat labai praktiška. To pavyzdys yra kodas »int div (int a, int b) meta« ⮩ »Aritmetikos išimtį {« ⮩ », jei (b == 0) {« ⮩ »mesti naują„ AithmeticException “(„ Padalinkite iš 0! “);„ ⮩ »} Kitas {« ⮩ »grįžti a / b;« ⮩ »}« ⮩ »}«. Metodo teiginys nusako, kokią išimtį metodas gali gauti. Metimo raktinis žodis nustato, kas turėtų nutikti su šia išimtimi.
- Praktiška: Viename bandymo bloke gali būti keli pagavimo blokai.
Naudokite „Java“ gijas
Gijos leidžia vienu metu paleisti skirtingas komandas „Java“. Yra du skirtingi gijų kūrimo būdai.
- Viena vertus, tam tikra klasė gali paveldėti klasę „Gija“. Pvz., Pirmiausia parašykite „klasės krautuvas praplečia giją {“. Tada jūs turite apibrėžti metodą su "public void run () {", kuris apibūdina gijos funkcijas. Kitoje klasėje dabar galite naudoti komandą „Krautuvas obj = naujas krautuvas ();“, kad pradėtumėte giją, naudodami metodą „obj.start ();“.
- Arba galite įdiegti „Runnable“ sąsają klasėje. Tai galite padaryti naudodami komandą "klasės krautuvas įgyvendina paleistą {". Dar kartą apibrėžkite vykdymo metodą. Dabar galite pradėti giją kitoje klasėje su kodu „Thread t = new Thread (new Loader ());“ ⮩ „t.start ();“
- Patarimas: Galite naudoti komandą „Thread.sleep (1000);“, kad sriegis galėtų pailsėti sekundę. Tačiau atkreipkite dėmesį, kad ši komanda sukuria „InterruptedException“.
Kurkite sąrašus „Java“
„Java“ yra du skirtingi sąrašų tipai: „ArrayList“ ir „LinkedList“.
- Paprastai „Java“ masyvai gali laikyti tik tam tikrą objektų skaičių. Tačiau „Java“ API yra funkcija, vadinama „ArrayList“. Pirmiausia parašykite „import java.util.ArrayList;“, kad galėtumėte importuoti reikiamą paketą. Dabar galite naudoti komandą ArrayList colours = new ArrayList () ;, kad galėtumėte sukurti naują sąrašą. Jei norite sukurti sąrašą, kuriame iš anksto nurodomas duomenų tipas ir dydis, galite naudoti komandą „ArrayList colours = new ArrayList (10);“.
- Dabar jūs galite ką nors įtraukti į sąrašą naudodami komandą »colors.add („ ... “);«. Naudojant »colors.remove („ ... “);« šį procesą galima pakeisti. Taip pat galite naudoti »colors.contains (...);«, norėdami sužinoti, ar sąraše yra tam tikras elementas, arba naudokite komandą »colors.get (int x);«, kad gautumėte vertę su tam tikru indeksu. Galiausiai, norėdami išklausyti sąrašo elementų skaičių, galite naudoti »colors.size ();«, o norėdami ištrinti visus sąrašo elementus, naudokite »colors.clear ();«.
- Taip pat yra „LinkedList“, kurį galite importuoti naudodami komandą »import java.util.LinkedList;«. Komandos čia tos pačios; tačiau jūs turite kiekvieną kartą rašyti „LinkedList“, o ne „ArrayList“. „ArrayList“ yra labiau tinkamas saugoti objektus, prie kurių bus galima pasiekti vėliau. Jei norite pakeisti duomenis, turėtumėte naudoti „LinkedList“.
„Java“: naudokite „HashMap“
Aukščiau pateiktuose sąrašuose kiekvienas objektas turi (sveikąjį) rodyklę. „HashMap“ yra naudojamas duomenims su poros reikšmių, susidedančių iš „Raktas“ ir „Objektas“, saugoti.
- „HashMap“ importuojate komanda „import java.util.HashMap;“. Panašiai kaip sąrašus, dabar galite sukurti „HashMap“ su komanda »HashMap points = new HashMap ();«.
- Tada, pavyzdžiui, galite pridėti komandą »points.put („ Amy “, 154);«. Komanda »System.out.println (" points.get ("Amy")); «dabar išveda reikšmę 154. Vėlgi, ją pašalinti galite naudoti šalinimo metodus.
- „HashMap“ negali būti objektų, turinčių du identiškus raktus. Objektas būtų paprasčiausiai perrašytas. Metodai „seesKey“ ir „saturValue“ yra labai praktiški ir gali būti naudojami norint patikrinti, ar objektas ar raktas egzistuoja „HashMap“.
- Taip pat yra vadinamųjų maišos rinkinių. Rinkinys yra reikšmių rinkinys, kuriame dvi identiškos vertės niekada negali egzistuoti. Importavimui naudojama komanda „java.util.HashSet;“. Pvz., Galite sukurti naują rinkinį, naudodami komandą »HashSet set = new HashSet ();«. Dabar prie šio „HashSet“ galite pridėti elementų, naudodami komandą »set.add (" ... ");«. Komanda »System.out.println (set);« pagaliau išveda visas rinkinio komandas.
- Paprastai elementų tvarka maišos rinkinyje nėra nuosekli. Tačiau, jei norite, galite naudoti „LinkedHashSet“.
- Beje: maišos lentelė saugo informaciją per mechanizmą, vadinamą „maiša“, kuriame kiekviena rakto reikšmė yra naudojama norint nustatyti unikalią vertę, vadinamą „maišos kodu“. Paprasčiau tariant, tai reiškia, kad kiekvienas maišos rinkinio elementas yra susietas su maišos kodu.
Rūšiuokite sąrašus ir naudokite iteratorius
Norėdami dar efektyviau dirbti su sąrašais, galite juos rūšiuoti ir naudoti iteratorius.
- „Java API“ taip pat apima kolekcijų klasę. Vienas iš praktiškiausių metodų yra rūšiavimo metodas. Visi kolekcijų klasės metodai yra statiniai metodai, todėl jų nereikia dar kartą deklaruoti. Pvz., Jei sukūrėte „ArrayList“ pavadinimu „gyvūnas“, galite užsakyti jo reikšmes naudodami komandą Collections.sort (gyvūnas); Kolekcijų klasę galite importuoti naudodami komandą »import java.util.Collections;«.
- Iteratorius yra objektas, kuris cirkuliuoja per kolekciją ir priima bei gali ištrinti reikšmes. Jūs importuojate reikiamą paketą naudodami komandą »import java.util.iterator;«. Šiuo atveju mes jau sukūrėme „LinkedList“, vadinamą „gyvūnais“. Dabar galite apibrėžti iteratorių naudodami komandą »Iterator it = animals.iterator ();«. Jei dabar vykdote komandą »System.out.println (it.next ());«, išvedamas pirmasis „LinkedList“ elementas. Jei komandą vykdote dar kartą, iteratorius žengia dar vieną žingsnį toliau ir išveda antrą elementą. Čia taip pat galite pašalinti elementus pašalinimo metodu. Taip pat yra metodas hasNext, kurį galite naudoti norėdami patikrinti, ar nėra kito elemento.
„Java“: sukurkite ir naudokite failus
„Java“ galite lengvai kurti ir naudoti savo failus.
- Norėdami tai padaryti, pirmiausia turite importuoti reikiamą paketą naudodami komandą „importuoti java.io.File;“. Tada apibrėžkite failą tinkamu keliu naudodami komandą »Failo failas = naujas failas („ C: \\ duomenys \\ įvesties failas.txt “);«. Naudodami »if (x.exists ()) {« galite patikrinkite, ar failas egzistuoja. Gaunate failo pavadinimą „x.getName ()“.
- Kitame žingsnyje galite sukurti naują skaitytuvą, naudodami komandą »Skaitytuvas sc = naujas skaitytuvas (x);«. Vėlgi, norėdami nuskaityti duomenis iš failo, galite naudoti tokias komandas kaip „sc.next ()“ arba „sc.hasNext ()“. Tai labai gerai veikia tam tikra kilpa. Nepamirškite uždaryti failo pabaigoje su „sc.close ();“.
- Norėdami rašyti į failus, pirmiausia turite pridėti komandą „importuoti java.util.Formatter;“. Komanda »Formatter f = naujas formatuotojas („ C: \\ CHIP \\ test.txt ");« nurodytu keliu sukuria failą pavadinimu „test.txt“.
- Norėdami rašyti į failą, galite naudoti komandą »f.format ("% s% s% s, "1", "Max", "Mustermann \ r \ n"); «. % s "eilutei, kuri pakeičiama pirmuoju parametru, antrajam„% s "antruoju parametru ir tt. Programa pereina į kitą eilutę paspausdama„ \ r \ n ".
Čia baigiasi mūsų mokymo programa apie „Java“ programavimo kalbą. Kitame praktiniame patarime pateiksime įvadą apie programavimą naudojant Python.