Java nima Java dasturlash tili


Download 1.25 Mb.
bet43/49
Sana20.01.2023
Hajmi1.25 Mb.
#1103409
1   ...   39   40   41   42   43   44   45   46   ...   49
Bog'liq
2 5420273372074344755

Ekranda


Oldin: HashSet contains: [Element1, Element2, Element3, Element4, Element5]
Keyin: HashSet contains: []

HashSet ni massivga convert qilish ga misol:


import java.util.HashSet;
class ConvertHashSettoArray{
public static void main(String[] args) {
// Create a HashSet
HashSet hset = new HashSet();
//add elements to HashSet
hset.add("Element1");
hset.add("Element2");
hset.add("Element3");
hset.add("Element4");
// Displaying HashSet elements
System.out.println("HashSet contains: "+ hset);
// Creating an Array
String[] array = new String[hset.size()];
hset.toArray(array);
// Displaying Array elements
System.out.println("Array elements: ");
for(String temp : array){
System.out.println(temp);
}
}
}

Ekranda:


HashSet contains: [Element1, Element2, Element3, Element4]
Array elements:
Element1
Element2
Element3
Element4

Hashsetga ArrayListga convert qilish:


import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
class ConvertHashSetToArrayList{
public static void main(String[] args) {
// Create a HashSet
HashSet hset = new HashSet();
//add elements to HashSet
hset.add("Steve");
hset.add("Matt");
hset.add("Govinda");
hset.add("John");
hset.add("Tommy");
// Displaying HashSet elements
System.out.println("HashSet contains: "+ hset);
// Creating a List of HashSet elements
List list = new ArrayList(hset);
// Displaying ArrayList elements
System.out.println("ArrayList contains: "+ list);
}
}

Ekranda


HashSet contains: [Tommy, Matt, Steve, Govinda, John]
ArrayList contains: [Tommy, Matt, Steve, Govinda, John]

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   49




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