使用Java; HashMaps和ArrayLists不在Eclipse或BlueJay中编译

时间:2012-05-24 15:11:17

标签: eclipse arraylist hashmap java

我无法让HashMaps和ArrayLists在我的计算机上正常运行。我尝试使用自己的代码,从教科书和在线复制样本以确保我的语法是正确的,但到目前为止,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中,转到Project-&gt; Properties-&gt; Java Compiler。我的java兼容性是JavaSE-1.7

- 在eclipse中,转到Window-&gt; Preferences-&gt; InstalledJREs。我有标准VM的jre7。

- 我试图右键单击我的包中的jre System Library,然后更改为JavaSE-1.6,1.7,并检查jre7的默认工作区框。

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

我在做傻事吗?我们非常感谢您提出的任何建议。谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

问题是代码不在任何方法中。在调用put方法的地方是声明变量的区域。看到这个修改过的代码我将变量设为静态,以便可以从main方法调用它。

public class StructureIssues {

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

public static void main(String args[]) {
    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");
}

}