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);
}
}
}
Do'stlaringiz bilan baham: