English 中文(简体)
new数组列表<T>(T[]arr)
原标题:new ArrayList<T> (T[] arr)
  • 时间:2012-05-25 16:55:43
  •  标签:
  • java

Okay So this doesnt make sense to me.... maybe someone can shed some light. A technique I figure out for converting primitive arrays into ArrayLists is as follows.

arr = new ArrayList<String>(Arrays.asList(primitveArray));

this works quit well. But today, I start wondering how efficent this was, since I happened to be writing some code where performance is more important then It had been in other applications I had worked on in the past and decided to look at the source code for Arrays.asList()

我发现了这个:

public static <T> List<T> asList(T... array) {
    return new ArrayList<T>(array);
}

我对自己说:“我真是个白痴,它只是将数组传递给ArrayList构造函数ArrayList<;T>;(T[]arr),为什么我不跳过一步,不使用愚蠢的Arrays.asList呢?

所以我试试这个

arr = new ArrayList<T>(primitveArray)

在我的代码中。但突然数组列表<;T>;(T[]arr)未定义。我很困惑为什么会这样?

最佳回答

但突然数组列表<;T>;(T[]arr)未定义。我很困惑为什么会这样?

有两个名为ArrayList的类:

  • one is a private static inner class of java.util.Arrays;
  • the other is java.util.ArrayList.

asList()使用前者。你试图使用后者。这两个类是不相关的,它们只是碰巧同名。

值得注意的是,java.util。数组。ArrayList不会复制数组。它提供了一个视角。

问题回答

好吧,在问了这个问题大约十秒钟后,我想出来了。熟练地java.util。数组实现了一个名为ArrayList的静态本地类,该类与java.util.util分开。ArrayList。令人困惑的是,它们都被命名为ArrayList,但在java.util.中。数组。ArrayList声明为

private static class ArrayList<E> extends AbstractList<E> implements
        List<E>, Serializable, RandomAccess

java.util。ArrayList声明为

public class ArrayList<E> extends AbstractList<E> implements List<E>,
    Cloneable, Serializable, RandomAccess 

我想现在的问题是,为什么会这样做。我想可能是优化。由于Array将ArrayList转换为List,因此它只需要支持足够的List操作,而不需要ArrayList的其他正常功能。

所以我尝试使用arr=new ArrayList<;T>;(primitveArray)

T是一个通用类型的参数,在实例化ArrayList时不能使用它,只能在类或方法中声明它的使用。相反,将代码更改为:

arr=Arrays.asList(primitveArray)

Arrays类中使用的ArrayList类与您在代码中使用的Array List类不同,它是Arrays类本身定义的内部类,并定义了一个以Array为参数的构造函数。由于该类是私有的,具有非公共构造函数,因此只能在数组类本身中声明和使用。

private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
    private final E[] a;

    ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
        a = array;
    }

...

我想现在的问题是,为什么会这样做

返回的列表是数组的视图,不会进行复制,对列表的任何更改都可以在源数组中看到。由于它始终由源数组支持,因此不支持任何影响列表大小的修改,并引发异常。

对你来说最重要的是要意识到Arrays.asList完全高性能的,它是O(1),具有固定开销。这是因为没有数据被复制。它只是给你的数组一个List接口来与它交互。其次,私有静态类ArrayList确实非常重要,因为公共类ArrayList确实会复制数据并导致性能下降。

我想现在的问题是,为什么会这样做。

Arrays.asList返回的List(一个Arrays.ArrayList

它只是一个适配器,可以有效地将基元数组视为List,而无需将元素复制到List

当你这样做的时候

new java.util.ArrayList( Arrays.asList( primitiveArray ) );

当您复制元素时。结果不是视图,而是独立于原始数组。此外,您还获得了调整其大小的能力。如果您查看源代码(您必须同时查看java.util.ArrayListArrays.ArrayList),您可以看到它正在以最有效的方式将这些元素从原始基元数组复制到支持java.util.ArrayList





相关问题
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 ...