Mając nieposortowaną listę ArrayList, zadaniem jest posortowanie tej listy ArrayList w kolejności rosnącej w Javie.
Przykłady:
Wejście : Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, portal komputerowy]
Wyjście : Sorted ArrayList: [Portal komputerowy, For, ForGeeks, Geeks, GeeksForGeeks]Wejście : Unsorted ArrayList: [Geeks, For, ForGeeks]
Wyjście : Sorted ArrayList: [For, ForGeeks, Geeks]ddl kontra dml
Zbliżać się: ArrayList można posortować za pomocą metody sort() klasy Collections w Javie. Ta metoda sort() przyjmuje jako parametr kolekcję, która ma zostać posortowana, i domyślnie zwraca kolekcję posortowaną w kolejności rosnącej.
Składnia:
Collections.sort(ArrayList);>
Poniżej implementacja powyższego podejścia:
dla każdego maszynopisu
// Java program to demonstrate> // How to sort ArrayList in ascending order> > import> java.util.*;> > public> class> GFG {> > public> static> void> main(String args[])> > {> > > // Get the ArrayList> > ArrayList> > list => new> ArrayList();> > > // Populate the ArrayList> > list.add(> 'Geeks'> );> > list.add(> 'For'> );> > list.add(> 'ForGeeks'> );> > list.add(> 'GeeksForGeeks'> );> > list.add(> 'A computer portal'> );> > > // Print the unsorted ArrayList> > System.out.println(> 'Unsorted ArrayList: '> > + list);> > > // Sorting ArrayList in ascending Order> > // using Collection.sort() method> > Collections.sort(list);> > > // Print the sorted ArrayList> > System.out.println(> 'Sorted ArrayList '> > +> 'in Ascending order : '> > + list);> > }> }> |
przekonwertuj nfa na dfa
>
>Wyjście:
Unsorted ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Sorted ArrayList in Ascending order : [A computer portal, For, ForGeeks, Geeks, GeeksForGeeks]>