English 中文(简体)
间歇方法 Arrays v.util List
原标题:Interface method agruments Arrays vs util List

我正在撰写一个接口及其实施。 接口方法类似

doSomething(String[] strs, Integer[] ints, String msg);

I declared parameters as arrays simply because it will call to an external interface having similar arguments. Some people suggest that doSomething agruments should be util List instead of arrays. But I couldn t find any best practice explains the reason reason why util List is preferable?

Loc

问题回答

清单更容易与它们合作,因为清单内容更为丰富,而且执行多种多样。 因此,概述是,它总体上更加灵活和可以维持。

Josh Bloch s Effective Java highlights one other reason to prefer Lists: "invariance". Generics are checked at compile time, so typed lists will actually catch more errors than arrays:

// Fails at runtime!
Object[] objectArray = new Long[1];
objectArray[0] = "I don t fit in"; // Throws ArrayStoreException
// Won t compile!
List<Object> ol = new ArrayList<Long>(); // Incompatible types
ol.add("I don t fit in");

因此,在某些情况下,在Arrays使用清单实际上比较安全。

它比这更重要,但开始难以解释。 See this connection to the relevant section of Effective Java, ch 5:

HTH

Basically list is abstract type and it need to be implemented again by any of its family members like ArrayList etc. So there is no much difference in using array and list in regarding performance, both are identical. Only in terms of maintainability we go for List interface and we can implement it for any family of List interface later based on the requirement.Also list provide flexible operations over array.

This falls under maintainability. You will find it very convenient to use the methods prepared for you.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签