Java nima Java dasturlash tili


import java.util.*; class


Download 1.25 Mb.
bet36/49
Sana20.01.2023
Hajmi1.25 Mb.
#1103409
1   ...   32   33   34   35   36   37   38   39   ...   49
Bog'liq
2 5420273372074344755

import java.util.*;
class TestCollection2{
 public static void main(String args[]){
ArrayList al=new ArrayList();
al.add("Zafar");
al.add("Jalol");
al.add("Fayzullo");
for(String obj:al)
System.out.println(obj);
 }
}
Ekaranda :
Zafar
Jalol
Fayzullo
 

ArrayList da saralash usullari

    1. Yuqoridan quyiga qarab

    2. Quyidan yuqoriga qarab

Yuqoridan quyiga qarab saralshga 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");

/*Unsorted List*/


System.out.println("Saralashdan oldin:");
for(String counter: listofcountries){
System.out.println(counter);
}

/* Sort statement*/


Collections.sort(listofcountries);

/* Sorted List*/


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 before the sorting*/
System.out.println("Saralashdan oldin:");
for(int counter: arraylist){
System.out.println(counter);
}

/* Sorting of arraylist using Collections.sort*/


Collections.sort(arraylist);

/* ArrayList after sorting*/


System.out.println("Saralashdan keyin:");
for(int counter: arraylist){
System.out.println(counter);
}
}
}

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   32   33   34   35   36   37   38   39   ...   49




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