java

Javada ArrayList klass

1.     Javada ArrayList klass, u ma’lumotlarni yig’ishda dinamik massivdan foydalanadi.  ArrayList AbstractList’dan nasl oladi va List ni implement qiladi.

2.     Javada ArrayList klass dublikat elementlarni ham saqlashi mumkin.

3.     Javada ArrayList klass tartib bilan elementlarni yozadi(index ketma-ketligi).

4.     Javada ArrayList klass sinxron emas.

5.     Javada ArrayList klassi ixtiyoriy kirishga (ixtiyoriy indexdagi elementni olish) ruxsat beradi chunki u asosi index bo’lgan massivda ishlaydi.

Java ArrayList klassida ma’lumotlarni boshqarish(o’chirish, yozish) sekin chunki elementlarni ko’chirish-o’tish jarayonlari ko’p sodir bo’ladi.

Javada umumiy(generic) bo’lmagan va umumiy(generic) bo’lgan to’plam(collection)lar

Javada umumiy bo’lmagan to’plam(collection)lar JDK 1.5  dan boshlab qo’shilgan.

Umumiy to’plam(collection)lar sizga faqat bir turdagi obyektlar bilan ishlashga ruhsat beradi.

Umumiy bo’lmagan arraylist yaratishga misol:         

    ArrayList al=new ArrayList();//umumiy bo’lmagan array list yaratish

Umuumiy bo’lgan arraylist yaratishga misol:

ArrayList<String> al=newArrayList<String>();//umumiy arraylist

Umumiy to’plam(collection)lar maxsus (<>) belgi bilan yaratiladi. Bunda ArrayList faqat bir turdagi obyektlar bilan ishlashini bildiradi. Agar biz boshqa turdagi obyektni qo’shsak  compile time error xatolik beradi

 

ArrayListga misol:

  1. import java.util.*;
  2.  
  3. class TestCollection1{
  4.  
  5. public static void main(String args[]){
  6.  
  7.  
  8. ArrayList<String> al=new ArrayList<String>();// arraylist yaratildi
  9.  
  10. al.add("Zafar");//obyekt qo’shilyapti
  11.  
  12. al.add("Jalol");
  13.  
  14. al.add("Fayzullo");
  15.  
  16. Iterator itr=al.iterator();
  17.  
  18. while(itr.hasNext()){
  19.  
  20. System.out.println(itr.next());
  21.  
  22. }
  23.  
  24. }
  25.  
  26. }

 

Ekranda:

Zafar

Jalol

Fayzullo

 

Javada to’plam(collection)larni iterate(tartib blan sanash)ni ikki xil usuli bor:

1.     Iterator interface orqali

2.     For-each orqali

Yuqoridagi misol iterator orqali elementlarni chiqarishga misol bo’ladi.

 

For-each oraqil iterate (tartib blan sanash) qilish

  1. import java.util.*;
  2.  
  3. class TestCollection2{
  4.  
  5. public static void main(String args[]){
  6.  
  7. ArrayList<String> al=new ArrayList<String>();
  8.  
  9. al.add("Zafar");
  10.  
  11. al.add("Jalol");
  12.  
  13. al.add("Fayzullo");
  14.  
  15. for(String obj:al)
  16.  
  17. System.out.println(obj);
  18.  
  19. }
  20.  
  21. }

 

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

  1. import java.util.*;
  2.  
  3. public class Details {
  4.  
  5.  
  6. public static void main(String args[]){
  7.  
  8. ArrayList<String> listofcountries = new ArrayList<String>();
  9.  
  10. listofcountries.add("India");
  11.  
  12. listofcountries.add("US");
  13.  
  14. listofcountries.add("China");
  15.  
  16. listofcountries.add("Uzbekistan");
  17.  
  18.  
  19. /* Saralanmagan ro'yhat */
  20.  
  21. System.out.println("Saralashdan oldin:");
  22.  
  23. for(String counter: listofcountries){
  24.  
  25. System.out.println(counter);
  26.  
  27. }
  28.  
  29.  
  30. /* Saralash ifodasi */
  31.  
  32. Collections.sort(listofcountries);
  33.  
  34.  
  35. /* Saralangan ro'yhat */
  36.  
  37. System.out.println("Saralashdan keyin:");
  38.  
  39. for(String counter: listofcountries){
  40.  
  41. System.out.println(counter);
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }

 

Ekranda

Saralashdan oldin:

India

US

China

Uzbekistan

Saralashdan keyin:

China

India

US

uzbekistan

 

Integer sortga misol:

  1. import java.util.*;
  2.  
  3. public class ArrayListOfInteger {
  4.  
  5.  
  6. public static void main(String args[]){
  7.  
  8. ArrayList<Integer> arraylist = new ArrayList<Integer>();
  9.  
  10. arraylist.add(11);
  11.  
  12. arraylist.add(2);
  13.  
  14. arraylist.add(7);
  15.  
  16. arraylist.add(3);
  17.  
  18. /* ArrayList saralashdan avval */
  19.  
  20. System.out.println("Saralashdan oldin:");
  21.  
  22. for(int counter: arraylist){
  23.  
  24. System.out.println(counter);
  25.  
  26. }
  27.  
  28.  
  29. /* Collections.sort 'dan foydalanib arrayList ni saralash*/
  30.  
  31. Collections.sort(arraylist);
  32.  
  33.  
  34. /* ArrayList saralashdan keyin*/
  35.  
  36. System.out.println("Saralashdan keyin:");
  37.  
  38. for(int counter: arraylist){
  39.  
  40. System.out.println(counter);
  41.  
  42. }
  43.  
  44. }
  45.  
  46. }

 

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

  1. import java.util.*;
  2.  
  3. public class Details {
  4.  
  5.  
  6. public static void main(String args[]){
  7.  
  8. ArrayList<String> arraylist = new ArrayList<String>();
  9.  
  10. arraylist.add("AA");
  11.  
  12. arraylist.add("ZZ");
  13.  
  14. arraylist.add("CC");
  15.  
  16. arraylist.add("FF");
  17.  
  18.  
  19. /* Saralanmagan List: ArrayList saralash avvalgi tarkibi */
  20.  
  21. System.out.println("Saralashdan oldin:");
  22.  
  23. for(String str: arraylist){
  24.  
  25. System.out.println(str);
  26.  
  27. }
  28.  
  29.  
  30. /* Kamayish tartibida saralash */
  31.  
  32. Collections.sort(arraylist, Collections.reverseOrder());
  33.  
  34.  
  35. /* Sorted List in reverse order*/
  36.  
  37. System.out.println("Saralangandan keyin:");
  38.  
  39. for(String str: arraylist){
  40.  
  41. System.out.println(str);
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }

 

Ekranda:

 

Saralashdan oldin:

AA

ZZ

CC

FF

Saralangandan keyin:

ZZ

FF

CC

AA

 

ArrayListda obyektlar bilan ishlash

  1. class Student{
  2.  
  3. int rollno;
  4.  
  5. String name;
  6.  
  7. int age;
  8.  
  9. Student(int rollno,String name,int age){
  10.  
  11. this.rollno=rollno;
  12.  
  13. this.name=name;
  14.  
  15. this.age=age;
  16.  
  17. }
  18.  
  19. }
  20.  
  21. import java.util.*;
  22.  
  23. public class TestCollection3{
  24.  
  25. public static void main(String args[]){
  26.  
  27.  
  28. Student s1=new Student(101,"Zafar",23);
  29.  
  30. Student s2=new Student(102,"Aziz",21);
  31.  
  32. Student s2=new Student(103,"Jalol",25);
  33.  
  34.  
  35. ArrayList<Student> al=new ArrayList<Student>();
  36.  
  37. al.add(s1);// Student klass obyekti qo’shilyapti
  38.  
  39. al.add(s2);
  40.  
  41. al.add(s3);
  42.  
  43.  
  44. Iterator itr=al.iterator();
  45.  
  46. while(itr.hasNext()){
  47.  
  48. Student st=(Student)itr.next();
  49.  
  50. System.out.println(st.rollno+" "+st.name+" "+st.age);
  51.  
  52. }
  53.  
  54. }
  55.  
  56. }

 

Ekranda:

101 Zafar 23

102 Aziz 21

103 Jalol 25

 

addAll() funksiyasi ya’ni ikkita listni bir-biri bilan qo’shishga doir misol:

  1. importutil.*;
  2.  
  3. classTestCollection4{
  4.  
  5. public static void main(String args[]){
  6.  
  7. ArrayList<String> al=new ArrayList<String>();
  8.  
  9. add("Aziz");
  10.  
  11. add("Zafar");
  12.  
  13. add("Jalol");
  14.  
  15. ArrayList<String> al2=newArrayList<String>();
  16.  
  17. add("Fayzullo");
  18.  
  19. add("Jamshid");
  20.  
  21. addAll(al2);
  22.  
  23. Iterator itr=al.iterator();
  24.  
  25. while(itr.hasNext()){
  26.  
  27. out.println(itr.next());
  28.  
  29. }
  30.  
  31. }
  32.  
  33. }

 

Ekranda :

Aziz

Zafar

Jalol

Fayzullo

Jamshid

 

Massivni ArrayListga qo’shishga doir ba’zi usullar

  1. import java.util.*;
  2.  
  3.  
  4. public class ArrayToArrayList {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. /*ArrayList e'lon qilish va qiymat berish*/
  9. String citynames[]={"Andijon", "Namangan", "Farg’ona", "Qo’qon"};
  10.  
  11. /*Array ni ArrayList'ga o'girish */
  12. ArrayList<String> citylist= new ArrayList<String>(Arrays.asList(citynames)); /*Adding new elements to the converted List*/ citylist.add("Pop"); citylist.add("Chust"); /*Final ArrayList content display using for*/ for (String str: citylist) { System.out.println(str); } } }

 

Ekranda:

Andijon

Namangan

Farg’ona

Qo’qon

Pop

Chust

 

Collections.addAll funksiyasidan foydalanib massivni Arraylist’ga qo’shish

  1. import java.util.*;
  2.  
  3.  
  4. public class Example2 {
  5.  
  6. public static void main(String[] args) {
  7.  
  8.  
  9. /*ArrayList e'lon qilish va qiymat berish*/
  10. String array[]={"Hi", "Hello", "Howdy", "Bye"};
  11.  
  12. /*ArrayList e'lon qilish*/
  13. ArrayList<String> arraylist= new ArrayList<String>();
  14.  
  15. /*O'girish*/
  16. Collections.addAll(arraylist, array);
  17.  
  18. /*o'girilgan List ga yangi element qo'shish*/
  19. arraylist.add("String1"); arraylist.add("String2");
  20.  
  21. /*massiv ro'yhatini ko'rsatish*/
  22. for (String str: arraylist) {
  23. System.out.println(str);
  24. }
  25. }
  26. }

 

Ekranda:

Hi

Hello

Howdy

Bye

String1

String2

 

Collection ning methodlaridan foydalanmasdan oddiy sikil yordamida qo’shish

  1. import java.util.*;
  2.  
  3.  
  4. public class Details {
  5.  
  6. public static void main(String[] args) {
  7.  
  8.  
  9. /*ArrayList e'lon qilindi*/
  10.  
  11. ArrayList<String> arraylist= new ArrayList<String>();
  12.  
  13.  
  14. /*Array qiymatlashtirildi*/
  15.  
  16. String array[] = {"Text1","Text2","Text3","Text4"};
  17.  
  18.  
  19. /*array.length massivda ko'rsatib turilgan
  20. elementlar sonini qaytaradi*/
  21.  
  22. for(int i =0;i<array.length;i++) { /* We are adding each array's element to the ArrayList*/ arraylist.add(array[i]); } /*ArrayList content*/ for(String str: arraylist) { System.out.println(str); } } }

 

Ekranda:

Text1

Text2

Text3

Text4

 

Endi esa yozgan jarayonlarimizni teskarisi yani ArrayListdan massivaga o’girish usullariga doir misollar

Collection ning methodlaridan foydalanilmagan holda ArrayListni massivga o’girish

  1. import java.util.*;
  2.  
  3.  
  4. public class ArrayListTOArray {
  5.  
  6. public static void main(String[] args) {
  7.  
  8.  
  9. /*ArrayList e'lon qilish va qiymat berish*/
  10. ArrayList<String> arrlist= new ArrayList<String>(); arrlist.add("String1"); arrlist.add("String2"); arrlist.add("String3"); arrlist.add("String4");
  11.  
  12.  
  13. /*ArrayList dan Array ga o'girish */
  14. String array[] = new String[arrlist.size()]; for(int j =0;j<arrlist.size();j++){ array[j] = arrlist.get(j); } /*Displaying Array elements*/ for(String k: array) { System.out.println(k); } } }

 

Ekranda:

String1

String2

String3

String4

 

toArray funksiyasidan foydalanib list’ni massivga o’girish

  1. import java.util.*;
  2.  
  3. public class Example {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. /*ArrayList e'lon qilish va qiymat berish*/
  8.  
  9. ArrayList<String> friendsnames= new ArrayList<String>();
  10.  
  11. friendsnames.add("Aziz");
  12.  
  13. friendsnames.add("Zafar");
  14.  
  15. friendsnames.add("Jalol");
  16.  
  17. friendsnames.add("Fayzullo");
  18.  
  19. /*ArrayList dan Array ga o'girish */
  20.  
  21. String frnames[]=friendsnames.toArray(new String[friendsnames.size()]);
  22.  
  23. /*Array elementlarini ko'rsatish*/
  24.  
  25. for(String k: frnames)
  26.  
  27. { System.out.println(k);
  28.  
  29. }
  30.  
  31. }
  32.  
  33. }

 

Ekranda:

Aziz

Zafar

Jalol

Fayzullo

G'ayratjon Rayimjonov
G'ayratjon Rayimjonov

G'ayratjon Rayimjonovning so'nngi maqolalari (hammasini ko'rish)

(85 marta o'qilgan, bugun 1 marta o'qildi)

O'xshash maqolalar: