- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在学习如何使用 java 进行编程,但对于 LinkedList
的 toArray(T[] a)
的区别我还没有得到任何明确的解释和 toArray()
方法。第二个简单地将 LinkedList 对象中的所有元素作为数组返回,对吧?但是,第一个呢?
编辑:
我的意思是,我阅读了 oracle 的文档,它说:
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the toArray() method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.
我不明白加粗显示的句子的意思。
最佳答案
假设您有一个 List<String>
,并且您想将其转换为 String[]
.让我们看看两种方法的工作原理:
List<String> source = new LinkedList<String>();
// Fill in some data
Object[] array1 = source.toArray();
String[] array2 = source.toArray(new String[source.size()]);
看出区别了吗?第一个简单地创建一个 Object[]
,因为它不知道类型参数的类型 <T>
,而第二个只是填满了 String[]
你通过了(这是你想要的)。您几乎总是需要使用第二种方法。
关于java - toArray(T[] a) 和 toArray() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28392705/
我一直在学习如何使用 java 进行编程,但对于 LinkedList 的 toArray(T[] a) 的区别我还没有得到任何明确的解释和 toArray() 方法。第二个简单地将 LinkedLi
查看 java 集合框架的 java api,我在 HashSet 中找不到 toArray() 方法,抽象类 Set 中有 toArray() 方法。 class Ideone { publ
对不起,如果我问的是愚蠢的问题,但任何人都可以解释以下两个调用 (ToArray) 之间的区别。在智能感知中,它不会将它们显示为重载方法,当然这两个调用的输出是相同的。 List i = new Li
考虑以下代码: List myList = Arrays.asList(1, 2, 3); String[] myArray1 = myList.toArray(new String[myList.s
我有一个ArrayList叫listtable 。由于某种原因Clause[] whatever = listtable.toArray()给出不兼容的类型错误,但 Clause[] whatever
我从不使用重载版本的 toArray Object[] toArray(Object[] a) 将集合转换为数组。 下面是我的javacode- public class Track { publi
我需要将 ID 列表转换为 ID 数组。我可以通过多种方式做到这一点,但不确定应该使用哪一种。 说, 1. ids.stream().toArray(Id[]::new) 2. ids.toArray
我试图通过在类中实现 List 接口(interface)来稍微调整 List。 我可以毫无问题地重写和实现所有方法,除了这个: T[] toArray(T[] a); Android Studio
我对 Node.js 和 MongoDB 非常陌生,正在尝试拼凑我自己的博客应用程序。我在尝试通过我的“博客”模型查询具有特定用户名的模型时遇到问题。当我尝试运行时: var userBlogs =
根据 toArray() 的 java 文档返回包含此集合中所有元素的数组。 和 toArray(Object obj[])。返回包含此集合中所有元素的数组;返回数组的运行时类型是指定数组的类型。 第
当尝试从 IEnumerable 对象集合中获取对象数组时(与我想要的数组不同的转换方式),我知道我可以先将源集合转换为正确的类型,然后从中获取数组,但是方法 ToArray() 给我的印象是它可以一
这些查询之间有区别吗?我很想知道 mongo 如何解释传递给 map 方法的 javascript 代码与查询解析后的映射。 db.collection('myCollection').find()
假设我有一个 ArrayList ArrayList myList; 我想调用toArray,是否有性能原因需要使用 MyClass[] arr = myList.toArray(new MyClas
我有一个 List在 Java 中,并希望将其转换为数组。 起初,我使用以数组作为输入的 toArray 语法,但后来我切换到无参数版本,即我从: String[] myArray = myList.
我最近在使用某种网络方法时遇到了很多问题: void CheckGfiHelpdesks(string ticket, GfiCheck[] newHelpdeskChecks, GfiCheck[]
我在我的项目中使用 angular-filter 按页面对输出对象进行排序,问题是当我使用这样的语法时: {{key}} {{dziecko.rodzina}} {{dzi
Laravel 5.8 我懒惰地将用户加载到与 crmaccount 对象具有一对一关系的相关客户 这些模型正在工作,因此当我检索预先加载的实体时,它会显示所有嵌套关系。 一行之后,我对该对象使用了“
我有一个简单的问题。 这是我的代码,问题是当我运行一些表情符号时,它会将它们显示为?,因为它将表情符号切成两半。 angular.module('Joe.filters').filter("initi
我想我已经陷入困境。由于性能原因,我正在尝试利用Java parallelStream。 函数Specimen.pick()采样并返回Specimen的实例。 我想在替换池时使用parallelStr
我写了这段代码,这是一个评级系统。我想要发生的是,当你将鼠标悬停在一颗星星上时,它应该触发之前的星星。 每当我将鼠标悬停在星星上时,图片就会发生变化,但它之前的星星不会改变。
我是一名优秀的程序员,十分优秀!