List <? extensions List <LatLng> How to use it

Asked 1 years ago, Updated 1 years ago, 65 views

List <? You want to use the datatype in extensions List <LatLng >>. But I don't know how to use it. Give me an example!

android list googlemap

2022-09-22 13:07

1 Answers

?extendsList<LatLng> means any type that inherits List<LangLng>.

For example, there is a code like below.

class LatLng {}
class LatLng2 extends LatLng {}
class LatLng3 extends LatLng {}

void print(List<LatLng> l) {}

List<LatLng> l1;
List<LatLng2> l2;
List<LatLng3> l3;

print(l1);
print(l2);
print(l3);

Compiling this code will result in errors in print(l2); and print(l3);. LatLng2 and LatLng3 can be cast after inheriting LatLng. However, List<LatLng> has no inheritance relationship with List<LatLng2> and List<LatLng3>. List does not care about the inheritance relationship of the element, so List<LatLng>, List<LatLng2> and LatLng3> are different types for

If so, you need to write print() for the three types, which can be duplicated and print() only wants to print out LatLng whether or not you inherit LatLng.

For this purpose, print() can be applied generic as below.

void print(List<? extends LatLng> l) {}

This conversion allows you to create a function that allows you to print(l1);, print(l2); and print(l3);.

Back to the point List <? How is extensionsList <LatLng >> used?

void func(List <? extends List <LatLng >> l) {
    // ..
}

List<ArrayList<LatLng>> l1;
List<LinkedList<LatLng>> l2;
List<LinkedList<LatLng>> l3;

func(l1);
func(l2);
func(l3);

If you look at the above code, l1, l2, and l3 are ArrayList or LinkedList what you want to do is to save LatLng in order. It's just that it's divided into two types of lists depending on the implementation. In the above code, List <? You can call func() whether ArrayList or LinkedList because you set it to extentsList <LatLng >>>. If List <List <LatLng >>, a call mode error occurs for l1, l2, and l3. This is because, as I explained earlier, the inheritance relationship of the element is not applied to List.


2022-09-22 13:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.