English 中文(简体)
与 Java 合作; HashMaps 和不在日光或蓝杰中编辑的阵列列表和阵列列表
原标题:Working with Java; HashMaps and ArrayLists not compiling in Eclipse or BlueJay

我很难让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比

- 重新安装日食Indigo几次,尝试64和32比特

- 日食时, 转到工程 & gt; Propertys- gt; Java 编译器。 我的 Java 兼容性为 JavaSE-1.7 。

- 日食时, 转到窗口 & gt; 偏好 & gt; Ins alled JREs。 我有标准 VM 的jre 7 。

- 我尝试过右键点击我的 jre 系统库的包, 并更改为 JavaSE- 1. 6, 1. 7, 并检查 Jre7 的默认工作空间框 。

- 我在蓝杰州尝试过类似的代码, 因为我想尝试另一个 IDE 来查看它是日蚀还是我的电脑。 我收到了: “ 识别符” 的预期值。 它突出显示了尝试Strings. add (“ 1 ”) ;

我在这里做什么傻事吗?

最佳回答

您的代码没有使用任何方法。 您可以在一个类中声明和初始化字段。 但使用这些字段应该使用方法( 或构建器) 。

问题回答

问题在于代码没有使用任何方法。 您调用配置方法的地方是您声明变量的区域。 见此修改的代码。 我做了变量静态, 以便从主方法中调用它 。

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

时 时





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签