0

I'm developing some methods here, some of them needs to have a list as a parameter.

I want to know if the appropriated way to do this is to use a List< T > or use an array [ ].

For example:

void method_name(List< String > arg)
void method_name(String arg[])

Which one is the recommended option?

Can someone help me?

duffymo
  • 293,097
  • 41
  • 348
  • 541
  • Is there a reason why not both? – John Hascall Jan 22 '16 at 10:10
  • If you need a list as parameters, then go for it, it is hard to say what to use without knowing the actual method. – LordAnomander Jan 22 '16 at 10:11
  • My doubt have started after I notice that Google is using array for the most part (actually I think it is for all) of its [Android APIs](http://developer.android.com/reference/packages.html). Maybe they have a reason for that, maybe it is the right choice. – Jorge Gabriel Siqueira Jan 22 '16 at 10:38
  • Like I said: context. Android is a web client. By the time those values are serialized to/from HTTP there's no advantage to List, because their numbers won't vary. By no means does that make arrays the right choice in all cases. If that were true, Joshua Bloch never would have written the Java Collections API. – duffymo Jan 22 '16 at 11:32
  • Nice explanation. In my case I DON'T want to change the values in the array parameter inside the method, because of this the array seems to be the best option. However, it seems to be more useful to use List, because an user will have more flexibility to manipulate it before passes it to my method. – Jorge Gabriel Siqueira Jan 22 '16 at 11:47

7 Answers7

1

There isn't a recommended or standard option. Lists and arrays are not the same object types at all. Both are used throughout Java. You can do either or both (overloading by type).

duffymo
  • 293,097
  • 41
  • 348
  • 541
1

Remember that List<T> is an interface. So passing a List as an argument makes your code more flexible since it does not depend on a specific implementation of a List.

So a method that takes a List<String> as a parameter can actually take an ArrayList<String> or a LinkedList<String> or any other implementation of the List interface. So it could even take a parameter of type MyList<String>, as long as the class MyList declares that it implements the List interface. The benefit of this is that if you wanted to change from using an ArrayList to a LinkedList elsewhere in your code, this method would still work.

By contrast, a method that takes a String[] can only take a String[]. So you would no longer have the benefit of being able to change the way you store these strings elsewhere in your code, without also having to change the method.

In terms of why Google might be using arrays as parameters a lot in their APIs, I think it really comes down to what they are using them for.

So I can't really recommend one or the other. It really depends on what the method does and what you want to do with the collection. For an overview of the key differences between modern programming structures, like Lists, and good old fashioned arrays, take a look at this answer.

Community
  • 1
  • 1
Redtama
  • 1,525
  • 1
  • 13
  • 34
0

Keep in mind that there is a third option available namely

void method(String... params)

I can be accessed like an array however the size is flexible and you do not have to put everything into an array before the method call, but simple pass all your Strings.

method(string1, string2, string3);
LordAnomander
  • 1,083
  • 6
  • 16
  • 1
    But be aware that you cannot pass this to a second method with varargs paramater. `void method2(String... params) { //params[0] will be an array of Strings //params[1] will give you an OutOfBoundsException } void method1(String... params) { //params[0] will be "first" //params[1] will be "second" method2(params); } method1("first", "second");` – lschuetze Jan 22 '16 at 10:38
0

i think more popular is array parameter. void method_name(String arg[]) you can get any element from array andunderstand how many elements in array.

DmitriyKhirniy
  • 245
  • 1
  • 7
0

I think you should use List. it is slower, but it offers more flexibility and it's easier to use, especially if you are going to resize them.

0

If your parameter list is fixed just use as many parameters as you need

method(String parameter1, String parameter2)

I find using Arrays is cumbersome. There are several shortcomings:

  • No generics, see here
  • you have to copy the whole array into a new bigger one if you want to enhance it
  • and more depending on what you want to do

Especially when you want to use Lists in your code you have to copy your content all the time. So I'd say to go with List.

method(List<String> parameterList)

There is mentioned another way using variable parameter lists (varargs). However, be aware that you cannot pass the vararg parameter simply into another method using varargs as the vararg parameter is represented as an array and will be passed as such.

method1("first", "second");

void method1(String... params) {
  //params[0] will be "first"
  //params[1] will be "second"
  method2(params);
}

void method2(String... params) {
  //params[0] will be an array of Strings
  //params[1] will give you an OutOfBoundsException
}
Community
  • 1
  • 1
lschuetze
  • 1,198
  • 9
  • 24
0

It completely depends on how you are going to use these objects.

Use List when

1) You are going to perform sorting, searching etc but not want to write much lines of code

2) If the size of elements may increase because Lists are resizeable.

Use Array if your requirement is not as above.

jcool
  • 178
  • 2
  • 10