如何在IntelliJ-IDEA中查看默认的Java类实现?

Lum*_*ria 1 java intellij-idea

有时我会阅读在线javadocs并仍然想知道特定方法是如何工作的.例如,我最近在查看用于Java-11的ArrayList在线javadoc,我无法判断"remove"方法是否缩短了数组的长度,或者只是将空值保留在曾经是元素的位置.

如何查看IntelliJ-IDEA中默认Java类的代码实现?

Swe*_*per 5

您可以在macOS上使用"Command + click",或者在光标位于方法上时在Windows或Linux上使用"Ctrl + click"来查看它的实现.如果您的插入符号位于方法名称内,您也可以键入"command/ctrl + B"而不是单击.如果它不明确,IntelliJ将允许您从列表中进行选择.

例如,要查看声明remove,您可以编写以下代码:

new ArrayList<String>().remove(1)
Run Code Online (Sandbox Code Playgroud)

然后命令+单击以查看其声明:

/**
 * Removes the element at the specified position in this list.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).
 *
 * @param index the index of the element to be removed
 * @return the element that was removed from the list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以右键单击并选择"转到 - >声明":

在此输入图像描述