7-ma’ruza Javada massivlar


ArrayList da saralash usullari


Download 45.28 Kb.
bet5/6
Sana15.06.2023
Hajmi45.28 Kb.
#1480022
1   2   3   4   5   6
Bog'liq
10-maruza

ArrayList da saralash usullari


1. Yuqoridan quyiga qarab
2. Quyidan yuqoriga qarab
Yuqoridan quyiga qarab saralashga misollar
Sting turdagi ArrayList yaratamiz va undagi ma'lumotlarni saralaymiz
import java.util.*;
public class Details {
public static void main(String args[])
{
ArrayList listofcountries = new ArrayList();
listofcountries.add("India");
listofcountries.add("US");
listofcountries.add("China");
listofcountries.add("Uzbekistan");
/* Saralanmagan ro'yhat */
System.out.println("Saralashdan oldin:");
for(String counter: listofcountries)
{
System.out.println(counter);
}
/* Saralash ifodasi */
Collections.sort(listofcountries);
/* Saralangan ro'yhat */
System.out.println("Saralashdan keyin:");
for(String counter: listofcountries)
{
System.out.println(counter);
}
}
}
Ekranda
Saralashdan oldin:
India
US
China
Uzbekistan
Saralashdan keyin:
China
India
US
uzbekistan

Integer sortga misol:


import java.util.*;
public class ArrayListOfInteger {
public static void main(String args[]){
ArrayList arraylist = new ArrayList();
arraylist.add(11);
arraylist.add(2);
arraylist.add(7);
arraylist.add(3);
/* ArrayList saralashdan avval */
System.out.println("Saralashdan oldin:");
for(int counter: arraylist)
{
System.out.println(counter);
}
/* Collections.sort 'dan foydalanib arrayList ni saralash*/
Collections.sort(arraylist);
/* ArrayList saralashdan keyin*/
System.out.println("Saralashdan keyin:");

Download 45.28 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling