Ekranda:
101 Zafar 23
102 Aziz 21
103 Jalol 25
addAll() funksiyasi yani ikkita listni bir biri bilan qo’shishga doir misol:
import java.util.*;
class TestCollection4{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add("Aziz");
al.add("Zafar");
al.add("Jalol");
ArrayList al2=new ArrayList();
al2.add("Fayzullo");
al2.add("Jamshid");
al.addAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
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
Do'stlaringiz bilan baham: |