简体   繁体   English

接口方法聚合数组与实用程序列表

[英]Interface method agruments Arrays vs util List

I am writing an interface and and its implementation. 我正在编写一个接口及其实现。 The interface has a method like 该接口具有类似

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. 有人建议doSomething聚合应该是util List而不是数组。 But I couldn't find any best practice explains the reason reason why util List is preferable? 但是我找不到任何最佳实践来说明util List更可取的原因?

Loc 位置

Lists are easier to work with, as they have a richer API, and a variety of implementations. 列表具有更丰富的API和各种实现,因此更易于使用。 So, the upshot is that it's generally more flexible and maintainable. 因此,结果是它通常更具灵活性和可维护性。

Josh Bloch's Effective Java highlights one other reason to prefer Lists: "invariance". 乔什·布洛赫(Josh Bloch)的《有效Java》(Effective Java)强调了选择列表的另一个原因:“不变性”。 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");

So, in some instances it's actually safer to use Lists over Arrays. 因此,在某些情况下,使用列表而不是数组实际上更安全。

There's more to it than that, but it starts getting difficult to explain. 不仅如此,但它开始变得难以解释。 See this link to the relevant section of Effective Java, ch 5: http://java.sun.com/docs/books/effective/generics.pdf 请参阅指向有效Java ch 5的相关部分的此链接: http : //java.sun.com/docs/books/effective/generics.pdf

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. 基本上,列表是抽象类型,它需要由其任何家族成员(如ArrayList等)再次实现。因此,在性能方面,使用数组和列表没有太大区别,两者是相同的。 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. 仅在可维护性方面,我们使用List接口,然后根据需要可以在以后的任何List接口家族中实现它.list还提供了对数组的灵活操作。

This falls under maintainability. 这属于可维护性。 You will find it very convenient to use the methods prepared for you. 您会发现使用为您准备的方法非常方便。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 集合接口与数组 - Collection Interface vs arrays 集合 vs 列表 vs arrays 作为 EJB 方法的返回类型 - collection vs list vs arrays as return type for EJB method java.lang.NullPointerException:尝试在空对象引用上调用接口方法“java.util.Iterator java.util.List.iterator()” - java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference 过滤器出错:尝试在空对象引用上调用接口方法&#39;java.util.Iterator java.util.List.iterator()&#39; - Error on filter: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference Android Filter a listview Attempt to invoke interface method &#39;int java.util.List.size()&#39; on a null object reference - Android Filter a listview Attempt to invoke interface method 'int java.util.List.size()' on a null object reference ExpandableListView:尝试在空对象引用上调用接口方法&#39;int java.util.List.size()&#39; - ExpandableListView: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 尝试在空对象引用上调用接口方法“int java.util.List.size()”——同时调用改造 - Attempt to invoke interface method 'int java.util.List.size()' on a null object reference - while calling retrofit 找不到符号符号方法 of() 定位接口 java.util.list, unittests - Cannot find symbol symbol method of() location interface java.util.list, unittests Android Studio错误:尝试在空对象引用上调用接口方法&#39;int java.util.List.size()&#39; - Android Studio Error : Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 尝试在空对象引用上调用接口方法&#39;int java.util.List.size()&#39; - Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM