开发者

Linked List in Java, How to cast String array into List?

开发者 https://www.devze.com 2023-03-09 18:06 出处:网络
Can you tell me what does this method return type is: List<passenger> recommend(Car a, Driver b){ ... }

Can you tell me what does this method return type is:

List<passenger> recommend(Car a, Driver b)  { ... }

I just want to know about Li开发者_如何转开发st keyword. Is this standard linked list or soemthing else.

If i have objects of passenger type. How can I add them in a List?


List in java is an interface. It means that it is not a concrete implementation, but the interface to it.

The concrete implementation the method can return can be LinkedList, ArrayList or any other class which implements the List interface. Read more in javadoc.

Basically, you add the elements to the list using methods add or addAll:

list.add(object);
list.addAll(anotherList);


List is an interface. it can be an ArrayList or what ever implements the List interface

List<passenger> list = new ArrayList<passenger>(); 

Basically, it contains a list of passengers.


List is an interface. It defines a common list of operations that all list types support.

The actual list may be backed by a linked list (LinkedList) or it may be backed by an array (ArrayList), or something else completely... you can find out if it's a LinkedList using the instanceof command (i.e. if (myList instanceof LinkedList) { /* Do something */ } )


I just want to know about List keyword.

First of all, List is not a keyword. It is the name of an interface.

So, what this method returns is a List. Which means that you do not know the underlying representation of the list.

It may be a linked list, or an array list, or some other type of list.

The idea behind returning an interface instead of a specified implementation is it better abstracts out the details of the method implementation. All that you need to know about the method is it will give you some object that behaves like a List.

See the List javadocs for more info. You my be particularly interested in the list of all known implementing subclasses.


Passenger p = new Passenger();
p.setId("1");
p.setName("xyz");
List<Passenger> passengerList = new ArrayList<Passenger>();
passengerList.add(p);


List is an interface. There are many implementations, one of which is LinkedList, it is all dependent on the concrete return type of your recommend method.

The List interface suggests an add(Object) method. With generics, you can pass a passenger object into your List and ensure type safety.


How to cast String array into List?

You can't. The word you are looking for here is convert.

0

精彩评论

暂无评论...
验证码 换一张
取 消