Java nima Java dasturlash tili


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

Ekranda:


101 Zafar 23
102 Aziz 21
103 Jalol 25

addAll() funksiyasi yani ikkita listni bir biri bilan qo’shishga doir misol:


  1. import java.util.*;

  2. class TestCollection4{

  3.  public static void main(String args[]){



  4. ArrayList al=new ArrayList();

  5. al.add("Aziz");

  6. al.add("Zafar");

  7. al.add("Jalol");



  8. ArrayList al2=new ArrayList();

  9. al2.add("Fayzullo");

  10. al2.add("Jamshid");



  11. al.addAll(al2);

  12. Iterator itr=al.iterator();

  13. while(itr.hasNext()){

  14. System.out.println(itr.next());

  15. }

  16.  }



  17. Ekranda :

Aziz
Zafar
Jalol
Fayzullo
Jamshid


Massivni ArrayListga qo’shish ga doir bazi usullarni

import java.util.*;


public class ArrayToArrayList {


public static void main(String[] args) {

/* Array Declaration and initialization*/


String citynames[]={"Andijon", "Namangan", "Farg’ona", "Qo’qon"};

/*Array to ArrayList conversion*/


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 method didan foydalanib massivni Arraylistag qo’shish


import java.util.*;

public class Example2 {


public static void main(String[] args) {

/* Array Declaration and initialization*/


String array[]={"Hi", "Hello", "Howdy", "Bye"};

/*ArrayList declaration*/


ArrayList arraylist= new ArrayList();

/*Conversion*/


Collections.addAll(arraylist, array);

/*Adding new elements to the converted List*/


arraylist.add("String1");
arraylist.add("String2");

/*Display array list*/


for (String str: arraylist)
{
System.out.println(str);
}
}
}
Ekranda:
Hi
Hello
Howdy
Bye
String1
String2


Download 1.25 Mb.

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




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