bet 35/41 Sana 30.12.2021 Hajmi 1,32 Mb. #90368
Bog'liq
Algoritmlash asoslari
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 < Student > al = new ArrayList < Student > () ;
al. add ( s1 ) ; // Student klass ob’yekti 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 < String > al = new ArrayList < String > () ;
add ( "Aziz" ) ;
add ( "Zafar" ) ;
add ( "Jalol" ) ;
ArrayList < String > al2 = newArrayList < String > () ;
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< String > citylist= new ArrayList< String >(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< String > arraylist= new ArrayList< String >();
/*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< array .length;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 e'lon qilish va qiymat berish*/
ArrayList< String > arrlist= new ArrayList< String >();
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
{
array[j] = arrlist.get(j);
}
/*Displaying Array elements*/
for ( String k: array)
{
System.out.println(k);
}
}
}
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< String > friendsnames= new ArrayList< String >();
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
24-dars. Javada LinkedList klassi
Javada LinkedList doubly linked listdan foydalanib elementlarni yig'adi. U AbstractList klassidan meros(extends) oladi va List va Deque interface'lardan implement oladi. ArrayListdan farqlaridan ham biri Deque dan implement olishidir;
LinkedList'da bir xil elementlar saqlash mumkin;
LinkedList'da ma'lumotlar kiritilish ketma-ketligida saqlanadi ;
LinkedList klass sinxron emas;
LinkedList klassda boshqarish jarayonlari tez sodir bo'ladi, chunki LinkedList'da elementlarni ko'chirib o'tkazilmaydi;
LinkedList list, stack yoki queue'lardan foydalanishi mumkin.
LinkedList da ham ma'lumotlar bilan ishlash ArrayList bilan deyarli bir xil amalga oshiriladi.
importutil. *;
publicclass TestCollection7 {
public static void main ( String args []){
LinkedList < String > al = new LinkedList < String > () ;
add ( "Zafar" ) ;
add ( "Aziz" ) ;
add ( "Jalol" ) ;
add ( "Zafar" ) ;
Iterator < String > itr = al. iterator () ;
while ( itr. hasNext ()){
out. println ( itr. next ()) ;
}
}
}
ArrayList saqlangan ma'lumotlarni LinkedList ham saqlashi mumkin
LinkedListda'gi ma'lumotlarni ArrayListga qo'shib qo'yishga misol
import java.util.ArrayList ;
import java.util.LinkedList ;
import java.util.List ;
Do'stlaringiz bilan baham: