The prazanList() metoda klase Java Collections koristi se za dobivanje popisa koji nema elemenata. Ove prazne liste su nepromjenjive prirode.
Sintaksa
Slijedi izjava od prazanList() metoda:
public static final List emptyList()
Parametar
Ova metoda ne prihvaća nijedan parametar.
Povratak
The prazanList() metoda vraća praznu nepromjenjivu listu.
Iznimke
DA
Verzija kompatibilnosti
Java 1.5 i novije
Primjer 1
import java.util.*; public class CollectionsEmptyListExample1 { public static void main(String[] args) { //Create an empty List List EmptyList = Collections.emptyList(); System.out.println('Empty list: '+EmptyList); } }Testirajte sada
Izlaz:
Empty list: []
Primjer 2
import java.util.*; public class CollectionsEmptyListExample2 { public static void main(String[] args) { //Create an empty List List emptylist = Collections.emptyList(); System.out.println('Created empty immutable list: '+emptylist); //Try to add elements emptylist.add('A'); emptylist.add('B'); } }Testirajte sada
Izlaz:
Created empty immutable list: [] Exception in thread 'main' java.lang.UnsupportedOperationException at java.base/java.util.AbstractList.add(AbstractList.java:153) at java.base/java.util.AbstractList.add(AbstractList.java:111) at myPackage.CollectionsEmptyListExample1.main(CollectionsEmptyListExample1.java:9)
Primjer 3
import java.util.*; public class CollectionsEmptyListExample3 { public static void main(String[] args) { //Create an empty List List empList = Collections.emptyList(); empList.add(1); empList.add(2); System.out.println('Created empty immutable List: '+empList); } }Testirajte sada
Izlaz:
Exception in thread 'main' java.lang.UnsupportedOperationException at java.base/java.util.AbstractList.add(AbstractList.java:153) at java.base/java.util.AbstractList.add(AbstractList.java:111) at myPackage.CollectionsEmptyListExample3.main(CollectionsEmptyListExample3.java:8)