有时我阅读在线 javadoc,仍然想知道特定方法是如何工作的。 例如,我最近正在查看 Java-11 的 ArrayList 在线 javadoc,我无法判断“删除”方法是否缩短了数组的长度,或者只是在曾经存在的地方留下了一个空值。元素。
如何查看IntelliJ-IDEA中默认Java类的代码实现?
当光标悬停在方法上时,您可以在 macOS 上使用“Command + 单击”,或者在 Windows 或 Linux 上使用“Ctrl + 单击”来查看其实现。如果插入符号位于方法名称内,您还可以键入“command/ctrl + B”而不是单击。如果不明确,IntelliJ 会让您从列表中进行选择。
例如,要查看
remove
的声明,可以编写以下代码:
new ArrayList<String>().remove(1)
然后命令+单击查看其声明:
/**
* 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;
}
或者,您可以右键单击并选择“转到 -> 声明”:
⌘+B 是查看任何 Java 选定类的完整源代码的快捷方式。
对于 Windows,选择类文本,然后按 Ctrl + B。