简体   繁体   English

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

[英]Working with Java; HashMaps and ArrayLists not compiling in Eclipse or BlueJay

I am having trouble getting HashMaps and ArrayLists to work correctly on my computer. 我无法让HashMaps和ArrayLists在我的计算机上正常工作。 I have tried using my own code, and copying samples from textbooks and online to make sure my syntax is correct, but thus far neither Eclipse or BlueJay will let me "add" or "put" things into these data structures. 我尝试使用自己的代码,并从教科书和在线复制示例以确保语法正确,但是到目前为止,Eclipse或BlueJay都不允许我“添加”或“放入”这些数据结构。 Here are some examples of what I have done. 这是我所做的一些示例。

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

} }

I did find one or two questions similar to mine, and I followed their advice, but so far I've had no luck. 我确实发现了一个或两个与我类似的问题,我听了他们的建议,但到目前为止我还没有运气。 Here is what I have tried to far to figure this out: 这是我试图弄清楚的事情:

-Reinstalled my JDK package several times, trying both 64 and 32-bit -多次重新安装我的JDK软件包,同时尝试64位和32位

-Reinstalled eclipse Indigo several times, trying both 64 and 32-bit -多次重新安装Eclipse Indigo,尝试64位和32位

-In eclipse, gone to Project->Properties->Java Compiler. -在Eclipse中,转到“项目”->“属性”->“ Java编译器”。 My java compliance is JavaSE-1.7 我的Java合规性是JavaSE-1.7

-In eclipse, gone to Window->Preferences->InstalledJREs. -在Eclipse中,转到“窗口”->“首选项”->“ InstalledJRE”。 I have jre7 with Standard VM. 我的标准VM有jre7。

-I have tried right clicking on my jre System Library in my packages, and changing to JavaSE-1.6, 1.7, and checking the default workspace box for jre7. -我尝试右键单击软件包中的jre系统库,然后更改为JavaSE-1.6、1.7,并检查jre7的默认工作区框。

-I have tried similar code in BlueJay because I wanted to try another IDE to see if it was eclipse or my computer. -我曾在BlueJay中尝试过类似的代码,因为我想尝试另一个IDE以查看它是日蚀还是我的计算机。 I received: "identifier" expected. 我收到:“标识符”预期。 It highlighted tryStrings.add("one"); 它突出显示了tryStrings.add(“ one”);。

Am I doing something silly here? 我在这里做傻事吗? Any advice you guys could offer would be greatly appreciated. 你们提供的任何建议将不胜感激。 Thank you for your time. 感谢您的时间。

Your code is not in any method. 您的代码没有任何方法。 You may declare and initialize fields in a class. 您可以在类中声明和初始化字段。 But using these fields should be done in methods (or constructors). 但是使用这些字段应该在方法(或构造函数)中完成。

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. 调用put方法的地方是声明变量的区域。 see this modified code. 看到这个修改后的代码。 I made the variable static so that it can be called from the main method. 我将变量设为静态,以便可以从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");
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM