有时我阅读了在线 javadoc,但仍然想知道特定方法是如何工作的。 例如,我最近在查看 Java-11 的 ArrayList 在线 javadoc,我无法判断“删除”方法是否缩短了数组的长度,或者只是在其中留下一个空值曾经有一个元素。
如何查看 IntelliJ-IDEA 中默认 Java 类的代码实现?
最佳答案
当光标悬停在方法上时,您可以在 macOS 上使用“Command + 单击”,或在 Windows 或 Linux 上使用“Ctrl + 单击”来查看它的实现。如果插入符在方法名称内,您也可以键入“command/ctrl + B”而不是单击。如果不明确,IntelliJ 会让您从列表中进行选择。
例如,要查看remove
的声明,可以这样写代码:
new ArrayList<String>().remove(1)
然后 command + 点击查看它的声明:
/**
* 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;
}
或者,您可以右键单击并选择“转到 -> 声明”:
关于java - 如何在 IntelliJ-IDEA 中查看默认的 Java 类实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953067/