1

我无法让 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->Properties->Java Compiler。我的 Java 合规性是 JavaSE-1.7

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

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

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

我在这里做傻事吗?你们可以提供的任何建议将不胜感激。感谢您的时间。

4

2 回答 2

1

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

于 2012-05-24T15:13:44.877 回答
0

Problem is that the code is not in any method. Where you are calling the put method is the area where you declare the variables. see this modified code. I made the variable static so that it can be called from the main method.

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");
}

}

于 2012-05-24T15:20:20.767 回答