17-dars. Java ArrayList, linkedList, HashMap, HashSet, Iterator Java ArrayList


Loop Through an ArrayList (ArrayList orqali aylanish)


Download 93.02 Kb.
Pdf ko'rish
bet2/7
Sana21.04.2023
Hajmi93.02 Kb.
#1372885
1   2   3   4   5   6   7
Bog'liq
17 dars java arraylist linkedlist hashmap hashset iterator XjkypEGJ

Loop Through an ArrayList (ArrayList orqali aylanish)
For tsikli bilan ArrayList elementlari bo'ylab aylanamiz va tsikl necha marta ishlashini belgilash uchun 
size() usulidan foydalanamiz:
public class Main {public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");for (int i = 0; i < cars.size(); i++) 
{System.out.println(cars.get(i));}}}
Bundan tashqari, ArrayList bo'ylab for-each orqali aylanishingiz mumkin:
public class Main {public static void main(String[] args) {
ArrayList cars = new ArrayList();


cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");for (String i : cars) {System.out.println(i);}}}
Other Types(Boshqa turlari)
ArrayList dagi elementlar aslida ob'ektlardir. Yuqoridagi misollarda biz "String" tipidagi elementlarni 
(ob'ektlarni) yaratdik. Esda tutingki, Java-dagi String ob'ektdir (primitive type emas). Int kabi boshqa 
turlardan foydalanish uchun siz ekvivalent sinfini ko'rsatishingiz kerak: Integer. Boshqa primitive typelar 
uchun quyidagilardan foydalaning: mantiqiy uchun Boolean, char uchun Character, double uchun 
Double va boshqalar:
Example
public class Main {public static void main(String[] args) {
ArrayList myNumbers = new ArrayList();
myNumbers.add(10);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(25);
for (int i : myNumbers) {
System.out.println(i);
}
}
}
Sort an ArrayList
Java.util paketidagi yana bir foydali sinf bu Collections klassi bo'lib, u Listlarni alifbo yoki son bo'yicha 
saralash uchun sort() usulini o'z ichiga oladi:
Example
Satrlar qatorini saralash:
public class Main {public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars); // Sort cars


for (String i : cars) {
System.out.println(i);
}
}
}
Butun sonlar qatorini saralash:
public class Main {
public static void main(String[] args) {
ArrayList myNumbers = new ArrayList();
myNumbers.add(33);
myNumbers.add(15);
myNumbers.add(20);
myNumbers.add(34);
myNumbers.add(8);
myNumbers.add(12);
Collections.sort(myNumbers); // Sort myNumbers
for (int i : myNumbers) {
System.out.println(i);
}
}
}

Download 93.02 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




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