2

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

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

I just want to know about List keyword. Is this standard linked list or soemthing else.
If i have objects of passenger type. How can I add them in a List?

4
  • 4
    Please, accept some answers for your previous questions by clicking a tick in the left of the answer. Commented May 31, 2011 at 13:48
  • I made a mistake by giving wrong email address. Now when i try to accept an answer it asks me to login and this email is invalid. What should i do? please tell me Commented May 31, 2011 at 13:50
  • 2
    Don't really know. Try to describe the problem on meta.stackoverflow.com Commented May 31, 2011 at 13:51
  • thanks for the information, i will do it now Commented May 31, 2011 at 14:52

7 Answers 7

5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

That means i will have to initialize a specific type List and then add elements to it and then return it.
3

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.

Comments

2

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 */ } )

Comments

1

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.

Comments

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

2 Comments

thanks a million times, actually this was exactly what i was looking for
Can you suggest me of if for example i have to make many passenger objects and then add them to list. HOw can i made multiple objects in a loop.
0

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.

Comments

0

How to cast String array into List?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.