An warware: raba saukowa

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.

Shafi posts:

Leave a Comment