在 Eclipse 中实现内容辅助中的类/子类

发布于 2024-12-10 20:29:44 字数 472 浏览 4 评论 0原文

我想做的是:

List<String> list = new 

然后按 Ctrl+Space 并使 ArrayList() (以及其他)显示在类型提案中。

我以为我以前可以使用此功能,但最近我不得不重新安装并且找不到它的设置。

这是 Eclipse Java EE helios,但如果需要,我可以升级到 indigo。

我尝试查找 在这里寻求帮助,但没有找到我正在寻找的信息。我尝试检查“默认提案类型”下的所有复选框(Java -> 编辑器 -> 内容辅助 -> 高级”,但无济于事。

What I'm trying to do is this:

List<String> list = new 

and then hit Ctrl+Space and get ArrayList<String>() (among others) to show up in the type proposal.

I thought I had this working previously, but I recently had to reinstall and can't find the setting for it.

This is Eclipse Java EE helios, but I can upgrade to indigo if need be.

I tried looking here for help, but didn't find the info I was looking for. I've tried checking all of the boxes under "Default Proposal Kinds" (Java -> Editor -> Content Assist -> Advanced" to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吹梦到西洲 2024-12-17 20:29:44

Eclipse 不知道哪个类实现了该接口,并且不会为其拥有的所有接口加载它们。但是,Eclipse 可以学习您使用的内容,并在下次使用时向您展示,也许这就是您发生的事情,随着您教 Eclipse 实现的类!

下面是学习之前/之后学习哪些类实现 Map 的 Eclipse 示例。

在此处输入图像描述

正如您在图像中看到的,第一次,Eclipse 除了 HashMap,我之前用过。

之后,我通过手动键入(仅限第一次)来使用 TreeMapLinkedHashMap,并且 Eclipse 现在缓存了它们。

正如人们所建议的,您可以将点放在 Map 上,然后单击 Ctrl+T 它将为所有实现此功能的类提供帮助。第一次会有帮助。

2014 年更新!

正如 @K.Carpenter 所注意到的,此功能在较新的 Eclipse 版本中被禁用。重新启用它。转到窗口 -> 首选项 -> Java -> 编辑器 -> 内容辅助 -> 高级。

Default Proposal Kinds下,您需要检查Java Type Proposals

Eclipse doesn't know which class implement the interface, and will not load them for all interfaces it has. BUT, Eclipse can learn what you use and show it to you on next use, maybe that's what happened to you, with time you taught Eclipse the implemented classes!

Here's an example of Eclipse before learning/and after learning what classes implement Map.

enter image description here

As you can see in the image, the first time, Eclipse didn't know anything other than HashMap, which I used before.

After that, I used TreeMap and LinkedHashMap by typing them manually (first time only) and Eclipse now cached them.

As the guys suggested, you can put the point on Map and click Ctrl+T it will give all the classes the implements this. Will be helpful the first time.

UPDATE in 2014!

As @K.Carpenter noticed, this feature is disabled in newer Eclipse versions. To re-enable it. Go to Window->Preferences->Java->Editor->Content Assist->Advanced.

Under Default Proposal Kinds, you will need to check Java Type Proposals

披肩女神 2024-12-17 20:29:44

像这样的代码是我最讨厌 Java 泛型的地方之一。我使用 Google 的 Guava 库来使我的泛型代码更具可读性,并且作为一个侧面 - effect 不需要 Eclipse 中的这个特定功能(尽管我同意应该实现它)。 Guava 对 Sets 也有类似的支持。

例如,我通常会声明我的代码如下:

import com.google.common.collect.Lists;
...
List myList<String> = Lists.newArrayList();

Code like this is one of my pet hates of Java Generics. I use Google's Guava libraries to make my generics code more readable, and as a side-effect don't need this particular feature in Eclipse (though I agree it should be implemented). Guava has similar support for Sets too.

For example, I would normally declare my code as follows:

import com.google.common.collect.Lists;
...
List myList<String> = Lists.newArrayList();
寄与心 2024-12-17 20:29:44

我希望看到 Eclipse 这样做,但我猜想如果没有起始字符,内容辅助就无法工作(除非有一些我们不知道的隐藏功能)。

好吧,考虑到这一点,为了解决“被最常用的实现锁定”的问题,我要做的就是查看“所有已知实现类”部分中的 javadoc,看看我可以使用的其他可能性。

我知道这不是 Eclipse 内的解决方案,但它可能会帮助一些遇到同样问题的用户。

I would like to see Eclipse doing this, but I guess that the content assist never worked without a start character (unless there is some hidden feature we don't know).

Well, having this in mind, what I do to workaround from 'getting locked with the most used implementations' is to look at the javadoc in the All Known Implementing Classes section, to see other possibilities that I could use.

I know it is not an in-Eclipse solution, but it may help some users that got stuck in the same problem.

自此以后,行同陌路 2024-12-17 20:29:44

这是一种可以向 Eclipse 添加新模板的方法,然后您所要做的就是输入 arraylist,按 ctrl + 空格键,它就会为您创建整个声明。您所要做的就是添加类型和名称。

保存此文件,然后将其导入到 eclipse

以下是导入方法/导出模板

  • 转到窗口>首选项>爪哇>编辑>模板

  • 选择您想要的模板。注意!复选框不表示
    选择了什么;它们用于启用/禁用模板。一个
    如果选择了表中的整行,则选择了模板,因此
    使用 Ctrl+左键单击或特定于您操作系统的方法进行多选
    模板。

  • 单击“导入...”并选择您收到的 XML 文件。或导出...和
    提供文件名。

类型数组列表

类型数组列表

按 ctrl+space 并选择数组列表

按 Ctrl + 空格

填写类型和名称
填写类型和名称

Here's a way that you can add a new template to eclipse, then all you have to do is type arraylist, press ctrl + space and it creates the entire declaration for you. All you have to do is add the type and name.

Save this file, then import it to eclipse

Here’s how to import/export a template

  • Go to Window > Preferences > Java > Editor > Templates

  • Select the templates you want. NB! The checkboxes don’t indicate
    what’s selected; they are used to enable/disable a template. A
    template is selected if the whole row in the table is selected, so
    use Ctrl+Left Click or method specific to your OS to multi-select
    templates.

  • Click Import… and choose the XML file you received. Or Export… and
    provide a filename.

Type arrayList


Type arraylist

Press ctrl+space and choose arraylist

Press Ctrl + Space

Fill in the type and name
Fill in the type and name

若水般的淡然安静女子 2024-12-17 20:29:44

不假装是您问题的答案,但我使用快速修复 (Ctrl+1/Ctrl+2 ) 定义新的局部变量或字段。

首先,我输入(可能使用 Ctrl+Space 进行内容辅助):

new ArrayList<String>();

然后按 Ctrl+2L 通过生成实例化类型的变量定义来将语句分配给新的局部变量:

ArrayList<String> arrayList = new ArrayList<String>();

最后,我使用 Tab(也可以使用 Enter)在插入的 arrayListArrayList 从下拉列表中指定确切的变量名称及其类型:

List<String> list = new ArrayList<String>();

第三次按 Tab 键将光标移动到语句末尾。

希望您也会发现这种方法很有用。

Not pretend to be an answer to your question but I use Quick Fix (Ctrl+1/Ctrl+2) to define a new local variable or field.

First, I type (maybe using Ctrl+Space for Content Assist):

new ArrayList<String>();

Then I press Ctrl+2 and L which assigns statement to new local variable by generating variable definition with type being instantiated:

ArrayList<String> arrayList = new ArrayList<String>();

Finally, I use tab (one may also use Enter) to navigate between inserted arrayList and ArrayList to specify the exact variable name and its type from drop-down list:

List<String> list = new ArrayList<String>();

Pressing tab for third time moves cursor to the end of statement.

Hope, you'll find this way useful too.

波浪屿的海角声 2024-12-17 20:29:44

我可以做到这一点,但它没有出现在类型提案中。尝试输入:

List; list = new Ar

按 Ctrl-Space 并接受第一个建议。对我来说,它完成了 ArrayList() (这是使用 SpringSource Tool Suite Eclipse 发行版)。

I am able to do this but it doesn't show up in the type proposal. Try typing:

List<String> list = new Ar

Hit Ctrl-Space and just accept the first suggestion. It completes to ArrayList<String>() for me (this is using the SpringSource Tool Suite Eclipse distribution).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文