();
/*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