Ekranda:
Saralashdan oldin::
11
2
7
3
Saralashdan keyin:
2
3
7
11
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
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);
}
}
}
Do'stlaringiz bilan baham: |