Java nima Java dasturlash tili


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

Ekranda:


Saralashdan oldin::
11
2
7
3
Saralashdan keyin:
2
3
7
11

Quyidan yuqoriga qarab saralash


Bu usulda elementlar quyi qisimdan yuoqri qisimga qarab saralanadi yani z dan a ga qarab








import java.util.*;
public class Details {

public static void main(String args[]){


ArrayList arraylist = new ArrayList();
arraylist.add("AA");
arraylist.add("ZZ");
arraylist.add("CC");
arraylist.add("FF");

/*Unsorted List: ArrayList content before sorting*/


System.out.println("Saralashdan oldin:");
for(String str: arraylist){
System.out.println(str);
}

/* Sorting in decreasing order*/


Collections.sort(arraylist, Collections.reverseOrder());

/* Sorted List in reverse order*/


System.out.println("Saralangandan keyin:");
for(String str: arraylist){
System.out.println(str);
}
}
}
Ekranda:

Saralashdan oldin:


AA
ZZ
CC
FF
Saralangandan keyin:
ZZ
FF
CC
AA

ArrayListda obyektlar bilan ishlash


class Student{
int rollno;
String name;
int age;
Student(int rollno,String name,int age){
this.rollno=rollno;
this.name=name;
this.age=age;
}
}
import java.util.*;
public class TestCollection3{
 public static void main(String args[]){

Student s1=new Student(101,"Zafar",23);
Student s2=new Student(102,"Aziz",21);
Student s2=new Student(103,"Jalol",25);

ArrayList al=new ArrayList();
al.add(s1);// Student class object qo’shilyapti
al.add(s2);
al.add(s3);

Iterator itr=al.iterator();
while(itr.hasNext()){
Student st=(Student)itr.next();
System.out.println(st.rollno+" "+st.name+" "+st.age);
}
 }


Download 1.25 Mb.

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




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