gpt4 book ai didi

eclipse - 使用 Java; HashMap 和 ArrayList 无法在 Eclipse 或 BlueJay 中编译

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:03 24 4
gpt4 key购买 nike

我无法让 HashMap 和 ArrayList 在我的计算机上正常工作。我尝试使用自己的代码,并从教科书和网上复制示例以确保我的语法正确,但到目前为止,Eclipse 或 BlueJay 都不会让我“添加”或“放入”内容到这些数据结构中。以下是我所做的一些示例。

package issues;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;

public class StructureIssues {

/*
* HashMap Attempt
*/
HashMap<Integer, String> numberNames = new HashMap<Integer, String>();

numberNames.put(new Integer(1), "hi");// here, I have syntax errors asking me to
// delete the (), and I have misplaced
// constructs on the dot.

//When the above line didn't work, I tried creating the objects
//outside of the parameter list...
Integer one = new Integer(1);
String myString = "hi";
numberNames.put(one, myString); //here it just complains about the parenthesis
//similar results for <String,String> and generic


/*
* ArrayList Attempt
*/
ArrayList<String> tryStrings = new ArrayList<String>();
String tryOne = "one";
tryStrings.add(tryOne);//Syntax error on tryOne; variable declarator ID expected
//also, syntax error on the dot; misplaced constructs

ArrayList<Integer> tryInts = new ArrayList<Integer>();
tryInts.add(new Integer(4));//Syntax error on add; expected "=" after it.

//Below, I have copied two lines from Big Java by Horstmann.
//The results are the same as my first String ArrayList attempt.
ArrayList<String> friends = new ArrayList<String>();
friends.add("Cindy");

}

我确实发现了一两个与我类似的问题,我听从了他们的建议,但到目前为止我还没有运气。以下是我为解决这个问题所做的努力:

-重新安装了我的 JDK 软件包几次,尝试了 64 位和 32 位

-重新安装了 eclipse Indigo 几次,尝试了 64 位和 32 位

-在 Eclipse 中,转到“项目”->“属性”->“Java 编译器”。我的 java 合规性是 JavaSE-1.7

-在 Eclipse 中,转到 Window->Preferences->InstalledJRE。我有带有标准 VM 的 jre7。

-我尝试右键单击包中的 jre 系统库,然后更改为 JavaSE-1.6、1.7,并选中 jre7 的默认工作区框。

-我在 BlueJay 中尝试过类似的代码,因为我想尝试另一个 IDE 来看看它是 eclipse 还是我的电脑。我收到:预期的“标识符”。它突出显示了 tryStrings.add("one");

我在这里做了什么傻事吗?你们能提供的任何建议将不胜感激。感谢您抽出时间。

最佳答案

您的代码不在任何方法中。您可以在类中声明和初始化字段。但使用这些字段应该在方法(或构造函数)中完成。

关于eclipse - 使用 Java; HashMap 和 ArrayList 无法在 Eclipse 或 BlueJay 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10740273/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com