Ich schrieb einen einfachen Codeblock zum Speichern fragments
mit ids
in Map
. Die value
und key
der Karte wird als nicht Nullable initialisiert, aber wenn ich sie als key
und value
erhalten möchte, gibt es nullable
Typ Daten. Also verstehe ich nicht warum? Gibt es einen anderen Kartentyp, der non-nullable
Daten in Kotlin
zurückgibt?Warum gibt mutableMapOf NULL zurück?
4
A
Antwort
9
Es ist möglich, dass es keinen Wert für die Taste [Position], das wird es null
machen zurück:
abstract operator fun get(key: K): V?
Gibt den Wert der gegebenen Taste entspricht, oder null wenn ein solcher Schlüssel nicht in der Karte vorhanden ist.
3
@nhaarman ist richtig, aber Sie können getValue(key)
verwenden, die Nicht-Nullable-Rückkehr aber NoSuchElementException
wenn nicht gefunden werfen.
/**
* Returns the value for the given [key] or throws an exception if there is no such key in the map.
*
* If the map was created by [withDefault], resorts to its `defaultValue` provider function
* instead of throwing an exception.
*
* @throws NoSuchElementException when the map doesn't contain a value for the specified key and
* no implicit default value was provided for that map.
*/
@SinceKotlin("1.1")
public fun <K, V> Map<K, V>.getValue(key: K): V = getOrImplicitDefault(key)
Verwandte Themen
- 1. Warum gibt getHandler() null zurück?
- 2. Warum gibt sharedInstance null zurück?
- 3. Warum gibt RedirectToAction null zurück?
- 4. Warum gibt getLocalName() null zurück?
- 5. Warum gibt getSelectedItem null zurück?
- 6. Warum gibt UILabel() null zurück?
- 7. Warum gibt Files.probeContentType Null zurück
- 8. Warum gibt staffel.getBounds() null zurück?
- 9. Warum gibt CMSampleBufferGetImageBuffer NULL zurück
- 10. Warum gibt SelectSingleNode null zurück?
- 11. C# XmlElement: Warum gibt NULL immer zurück?
- 12. Warum gibt die Funktion gmtime() NULL zurück?
- 13. Warum gibt meine Stub-Methode null zurück?
- 14. Warum gibt checkSelfPermissions() null zurück? (Android)
- 15. Warum gibt die gemeinsame Einstellung null zurück?
- 16. Warum gibt rawQuery() null Zeilen zurück?
- 17. Warum gibt diese Schleife NULL zurück
- 18. PHP - Warum gibt meine Abfrage NULL zurück?
- 19. getenv ("cc") gibt NULL zurück, warum?
- 20. Warum gibt die listFiles() -Methode null zurück?
- 21. Warum gibt diese MySQL-Funktion null zurück?
- 22. Warum gibt WebResourceResponse null in shouldInterceptRequest() zurück?
- 23. URI - getHost gibt null zurück. Warum?
- 24. Warum FragmentManager getBackStackEntryCount() gibt Null zurück?
- 25. Warum gibt diese Modusfunktion null zurück?
- 26. AngularJS- document.getElementById() - Warum gibt Element null zurück?
- 27. Warum gibt diese Stored Procedure NULL zurück
- 28. Parsen von JSON gibt null zurück, warum?
- 29. Warum gibt JSONObject.get ("name") null zurück?
- 30. Warum gibt locationManager.getBestProvider() null zurück? (Nexus 5x)
Vor Ihrer Antwort schrieb ich dies. : '' '' override Spaß getItem (Position: Int): Fragment = (fragmentsAndIds [Position])?: throw NullPointerException() '' '' Es macht ähnliches. Aber Ihre Option ist sauberer und kurz. Ich werde es benutzen. danke – emiraslan
Ja, es macht das Gleiche. Übrigens kannst du 'override fun getItem (position: Int) schreiben: Fragment = (fragmentsAndIds [position]) !!'. Das '!!' wird 'NullPointerException' werfen, wenn null;) –