简体   繁体   English

任何人都可以帮助我解决Java中的这个错误吗?

[英]can any one help me in this error in java?

i create hash table in java but there is aproblem in it when adding elements in it.我在java中创建了哈希表,但是在其中添加元素时存在问题。

Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
 hashT.put<sum2 , g>;

the error in second line where define sum2 is variable contain integer value and g variable contain string value.第二行中的错误定义 sum2 是变量包含integer数值,g 变量包含string值。 i dont know where the problem in putting elements in hash table.i want to add the values of this variables in hash table each time as the values change.我不知道将元素放入哈希表的问题在哪里。我想每次随着值的变化而在哈希表中添加这个变量的值。

To mutate an object, you have to call its properties.要改变一个对象,你必须调用它的属性。 An object property is either a field or a method.一个对象属性要么是一个字段,要么是一个方法。 In your case put is a method of the hashT object.在您的情况下puthashT对象的一个​​方法。 A method call is done by writing the object name, then the dot operator, then the method name and finally the arguments surrounded by parenthesis:方法调用是通过编写对象名称,然后是点运算符,然后是方法名称,最后是用括号括起来的参数来完成的:

objectName.methodName(argument1, argument2, ...);

The problem is here:问题在这里:

hashT.put<sum2 , g>;

put is a method and to call it you have to surround the arguments ( sum2 and g ) with parenthesis: put是一种方法,要调用它,您必须用括号将参数( sum2g )括起来:

hashT.put(sum2, g);

You need to follow the comments and edit your title so its a meaningful title that will help other people understand what your question is about.您需要关注评论并编辑您的标题,使其成为一个有意义的标题,以帮助其他人了解您的问题。

Also, when you get an error, copy the big red text that is displayed in the console (color depends on which editor you are using), also known as the error's "Stack Trace" and past it into your question somewhere.此外,当您遇到错误时,复制控制台中显示的大红色文本(颜色取决于您使用的编辑器),也称为错误的“堆栈跟踪”,并将其粘贴到您的问题中。 This will help us pinpoint what is going on, and the error title itself will likely give away to us exactly what's wrong.这将帮助我们查明发生了什么,并且错误标题本身可能会告诉我们究竟出了什么问题。

However, without any context on what the error is and what is before or after those two lines of code it is difficult to determine if you have previously defined sum2 or g as a variable that stores values.但是,如果没有关于错误是什么以及这两行代码之前或之后的任何上下文,则很难确定您之前是否已将sum2g定义为存储值的变量。 I am going to assume you haven't assigned at least one of them, likely g as a variable.我将假设您尚未分配至少其中一个,可能是g作为变量。

For experimental purposes, try replacing those two lines of code with:出于实验目的,请尝试将这两行代码替换为:

Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
 hashT.put<0 , "g">;

That is putting zero ( 0 ) as an explicit integer and g as an explicit string into the hashtable.这就是将零 ( 0 ) 作为显式整数和g作为显式字符串放入哈希表中。 If you need to put variables into there, then you need to define them before hand, like this:如果您需要将变量放入那里,那么您需要事先定义它们,如下所示:

int sum2 = 3 + 4;
String g = "Some String";

Hashtable <Integer,String> hashT=new Hashtable<Integer ,String >();
hashT.put<sum2 , g>;

Now the integer value 7 is stored as the hash and the string Some String as the mapped value.现在整数值7存储为散列值,字符串Some String存储为映射值。

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

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