Java nima Java dasturlash tili


Collection ning methodlaridan foydalanmasdan oddiy sikil yordamida qo’shish


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

Collection ning methodlaridan foydalanmasdan oddiy sikil yordamida qo’shish
import java.util.*;

public class Details {


public static void main(String[] args) {

/*ArrayList declaration*/


ArrayList arraylist= new ArrayList();

/*Initialized Array*/


String array[] = {"Text1","Text2","Text3","Text4"};

/*array.length returns the current number of


* elements present in array*/
for(int i =0;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
import java.util.*;

public class ArrayListTOArray {


public static void main(String[] args) {

/*ArrayList declaration and initialization*/


ArrayList arrlist= new ArrayList();
arrlist.add("String1");
arrlist.add("String2");
arrlist.add("String3");
arrlist.add("String4");

/*ArrayList to Array Conversion */


String array[] = new String[arrlist.size()];
for(int j =0;j array[j] = arrlist.get(j);
}

/*Displaying Array elements*/


for(String k: array)
{
System.out.println(k);
}
}
}
Ekranda:
String1
String2
String3
String4


toArray methodidan foydlanib listni masiga o’girish
import java.util.*;

public class Example {


public static void main(String[] args) {

/*ArrayList declaration and initialization*/


ArrayList friendsnames= new ArrayList();
friendsnames.add("Aziz");
friendsnames.add("Zafar");
friendsnames.add("Jalol");
friendsnames.add("Fayzullo");

/*ArrayList to Array Conversion */


String frnames[]=friendsnames.toArray(new String[friendsnames.size()]);

/*Displaying Array elements*/


for(String k: frnames)
{
System.out.println(k);
}
}
}
Ekranda:
Aziz
Zafar
Jalol
Fayzullo


Download 1.25 Mb.

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




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