22-dars — Javada ArrayList klass.

22-dars — Javada ArrayList klass.

22-dars - 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 al=newArrayList();//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

import java.util.*;
 
class TestCollection1{
 
    public static void main(String args[]){ 
 
        ArrayList al=new ArrayList();// arraylist yaratildi 
        al.add("Zafar");//obyekt qo'shilyapti 
        al.add("Jalol"); 
        al.add("Fayzullo"); 
        Iterator itr=al.iterator(); 
        while(itr.hasNext())
        { 
            System.out.println(itr.next()); 
        }
    } 
}

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

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 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:"); 
        for(int counter: arraylist)
        { 
            System.out.println(counter); 
        } 
    } 
}

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"); 
 
        /* Saralanmagan List: ArrayList saralash avvalgi tarkibi */
 
        System.out.println("Saralashdan oldin:"); 
        for(String str: arraylist)
        {
            System.out.println(str); 
        }  
        /* Kamayish tartibida saralash */ 
        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 klass obyekti 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); 
        } 
    }
}

Ekranda:

101 Zafar 23

102 Aziz 21

103 Jalol 25

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

importutil.*;
 
classTestCollection4{
 
    public static void main(String args[]){ 
        ArrayList al=new ArrayList(); 
        add("Aziz"); 
        add("Zafar"); 
        add("Jalol"); 
        ArrayList al2=newArrayList(); 
        add("Fayzullo"); 
        add("Jamshid"); 
        addAll(al2); 
        Iterator itr=al.iterator(); 
        while(itr.hasNext())
        { 
            out.println(itr.next()); 
        } 
    } 
}

Ekranda :

Aziz

Zafar

Jalol

Fayzullo

Jamshid

Massivni ArrayListga qo’shishga doir ba’zi usullar

import java.util.*; 
 
public class ArrayToArrayList { 
    public static void main(String[] args) { 
        /*ArrayList e'lon qilish va qiymat berish*/
        String citynames[]={"Andijon", "Namangan", "Farg'ona", "Qo'qon"};  
        /*Array ni ArrayList'ga o'girish */ 
        ArrayList citylist= new ArrayList(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

import java.util.*; 
 
public class Example2 {
 
    public static void main(String[] args) { 
 
        /*ArrayList e'lon qilish va qiymat berish*/
        String array[]={"Hi", "Hello", "Howdy", "Bye"};  
        /*ArrayList e'lon qilish*/ 
        ArrayList arraylist= new ArrayList(); 
        /*O'girish*/ 
        Collections.addAll(arraylist, array);  
        /*o'girilgan List ga yangi element qo'shish*/ 
        arraylist.add("String1"); arraylist.add("String2");  
        /*massiv ro'yhatini ko'rsatish*/ 
        for (String str: arraylist) 
        { 
            System.out.println(str); 
        } 
    }
}

Ekranda:

Hi

Hello

Howdy

Bye

String1

String2

Collection ning methodlaridan foydalanmasdan oddiy sikil yordamida qo’shish

import java.util.*; 
 
public class Details {
 
    public static void main(String[] args) { 
        /*ArrayList e'lon qilindi*/ 
        ArrayList arraylist= new ArrayList(); 
        /*Array qiymatlashtirildi*/ 
        String array[] = {"Text1","Text2","Text3","Text4"}; 
        /*array.length massivda ko'rsatib turilgan elementlar sonini qaytaradi*/ 
 
        for(int i =0;i

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

import java.util.*; 
 
public class ArrayListTOArray {
 
    public static void main(String[] args) { 
 
        /*ArrayList e'lon qilish va qiymat berish*/
        ArrayList arrlist= new ArrayList(); 
        arrlist.add("String1"); 
        arrlist.add("String2"); 
        arrlist.add("String3"); 
        arrlist.add("String4");
 
        /*ArrayList dan Array ga o'girish */
        String array[] = new String[arrlist.size()]; 
        for(int j =0;j

Ekranda:

String1

String2

String3

String4

toArray funksiyasidan foydalanib list'ni massivga o'girish

import java.util.*;
 
public class Example {
 
    public static void main(String[] args) { 
        /*ArrayList e'lon qilish va qiymat berish*/ 
        ArrayList friendsnames= new ArrayList(); 
        friendsnames.add("Aziz"); 
        friendsnames.add("Zafar"); 
        friendsnames.add("Jalol"); 
        friendsnames.add("Fayzullo"); 
        /*ArrayList dan Array ga o'girish */ 
        String frnames[] = friendsnames.toArray(new String[friendsnames.size()]); 
        /*Array elementlarini ko'rsatish*/ 
        for(String k: frnames) 
        {                          
            System.out.println(k); 
        } 
    } 
}

Ekranda:

Aziz

Zafar

Jalol

Fayzullo

Manba:

Umumiy Dasturlash
22-dars — Javada ArrayList klass.