有时,我阅读在线javadocs,仍然想知道一个特定的方法是如何工作的。例如,我最近查看了Java-11的ArrayList online javadocforJava-11,我无法判断“删除”方法是缩短了数组的长度,还是在过去有一个元素的地方只留下一个空值。
如何查看IntelliJ中默认Java类的代码实现?
发布于 2018-12-28 02:40:15
您可以在macOS上使用"Command + click“,或者在Windows或Linux上使用"Ctrl + click”,而游标在方法上以查看它的实现。如果插入符号位于方法名称内,也可以键入“命令/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;
}或者,您可以右击并选择“转到->声明”:

发布于 2018-12-28 02:47:00
如果您正在使用Mac,⌘+B是查看任何的完整源代码的快捷方式。
https://stackoverflow.com/questions/53953067
复制相似问题