Rarraba ra'ayi ne mai matuฦar mahimmanci a cikin haษaka software. Yana jujjuya zuwa tsara bayanai a cikin wani tsari na musamman, kuma ga mai tsara shirye-shirye, yana da mahimmanci don fahimtar yadda ake aiwatar da daidaitattun algorithms. ฦayan irin wannan hanyar da ake yawan amfani da ita ita ce rarrabuwar abubuwa na tsararru a cikin tsari mai saukowa. Wannan tsari da gaske ya ฦunshi tsara abubuwa daga mafi girma zuwa mafi ฦasฦanci ko a cikin tsari mai raguwa. Wannan labarin zai jagorance ku ta hanyar mafita don warware tsararru a cikin tsari mai saukowa ta amfani da Java. Dangane da Java, yana ba da hanyoyi da yawa don tsara tsararru a cikin oda daban-daban. Amma menene muke yi lokacin da muke buฦatar tsara tsararru a cikin tsari mai saukowa saboda babu wata hanyar kai tsaye da ake da ita? Amsar ita ce muna amfani da hanyar Arrays.sort() don tsara tsararru a cikin tsari mai hawa sannan mu juya ta ta amfani da hanyar Collections.reverse(). Ta wannan hanyar za mu iya tsara tsararru a cikin tsari mai saukowa.import java.util.Arrays; import java.util.Collections; public class MainClass{ public static void main(String[] args) { Integer[] array = {12, 4, 5, 2, 5}; // Sorting array in ascending order Arrays.sort(array); Collections.reverse(Arrays.asList(array)); System.out.println(Arrays.asList(array)); } }Shirin Java da aka bayar na sama yana tsara abubuwan tsararru a cikin tsari mai saukowa, ษaukar tsararrun tsararru azaman shigarwa sannan kuma buga jeri a cikin na'ura mai kwakwalwa.
Rushewar Code
Za mu fara da shigo da dakunan karatu da ake buฦata, watau `java.util.Arrays` & `java.util.Collections`. Ajin 'Arrays' a cikin kunshin java.util wani yanki ne na Tsarin Tarin Java kuma yana ba da hanyoyin amfani don tsararraki. Ya ฦunshi hanyoyi daban-daban na tsaye don rarrabuwa da bincika jeri-jeri, kwatanta jeri-jeri, da cike abubuwan tsararru.
Ajin 'Tarin' ya ฦunshi tsayayyen hanyoyi waษanda ke aiki akan ko dawo da tarin. Yana ba da hanyoyin amfani gabaษaya da yawa kamar hanyar Collections.reverse() wanda ke juyar da tsari na abubuwan da ke cikin ฦayyadadden jeri.
โข 'MainClass' shine ajin da aka ayyana mai amfani wanda ya ฦunshi babbar hanyar.
A cikin babbar hanyar, da farko za mu ฦirฦiri tsararru kuma mu fara shi da wasu abubuwa.
โข Sannan, muna amfani da hanyar Arrays.sort() don tsara tsararru a cikin tsari mai hawa.
Daga baya, muna jujjuya jeri ta amfani da hanyar Collections.reverse() don samun abubuwan da ke sauka.
โข Daga ฦarshe, muna buga tsararrun tsararru (watau a cikin tsari mai saukowa).
Maษallin Laburaren Java Don Rarraba
Java yana ba da ษimbin ษakunan karatu da hanyoyin da ke sa aiwatar da nau'ikan ayyuka su zama iska. Wasu mabuษin sune:
- 'java.util.Arrays': Wannan shine ษayan ษakin karatu na Java da aka fi amfani dashi don ayyukan tsararru. Yana ba da hanyoyin amfani da yawa don Arrays, gami da nau'i, binciken binary, kwatanta, cika, lambar zanta, da sauransu.
- `java.util.Collections': Wannan aji ne na taimako wanda ke aiki akan tarin kuma yana ba da hanyoyin amfani don sarrafa tarin.
Fahimtar waษannan mahimman ษakunan karatu da hanyoyi suna da mahimmanci don aiwatar da ayyukan rarrabuwa a Java. Hakanan, yana da fa'ida sanin waษannan don inganta ingantaccen lambar ku.