Java 无法从类型对象转换为 long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2351189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:27:34  来源:igfitidea点击:

Cannot convert from type object to long

javacastingtypeslong-integer

提问by Mohit BAnsal

I have a hashtable named table. The type value is long. I am getting values using .values(). Now I want to access these values.

我有一个名为table. 类型值为long。我正在使用.values(). 现在我想访问这些值。

Collection val = table.values();

Iterator itr = val.iterator();
long a  =   (long)itr.next();

But when I try to get it, it gives me error because I can't convert from type objectto long. How can I go around it?

但是当我尝试获取它时,它给了我错误,因为我无法从类型转换objectlong. 我怎样才能绕过它?

回答by Vincent Ramdhanie

Try this:

尝试这个:

  Long a = (Long)itr.next();

You end up with a Long object but with autoboxing you may use it almost like a primitive long.

您最终会得到一个 Long 对象,但通过自动装箱,您几乎可以像使用原始 long 对象一样使用它。

Another option is to use Generics:

另一种选择是使用泛型:

  Iterator<Long> itr = val.iterator();
  Long a = itr.next();

回答by missingfaktor

Try : long a = ((Long) itr.next()).longValue();

尝试 : long a = ((Long) itr.next()).longValue();

回答by Desintegr

You should use the newGenericsfeatures from Java 5.

您应该使用Java 5 中的泛型特性。

When you take an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

当您从集合中取出元素时,您必须将其转换为存储在集合中的元素类型。除了不方便之外,这是不安全的。编译器不会检查您的转换是否与集合的类型相同,因此转换可能会在运行时失败。

泛型为您提供了一种将集合的类型传达给编译器的方法,以便可以对其进行检查。一旦编译器知道集合的元素类型,编译器就可以检查您是否一致地使用了集合,并且可以对从集合中取出的值插入正确的转换。

You can read this quick howtoor this more complete tutorial.

你可以阅读这个快速HOWTO或本更完整的教程

回答by ufk

in my case I have an array of Objects that I get from a flex client,

就我而言,我有一个从 flex 客户端获取的对象数组,

sometimes the numbers can be interpreted by java as int and sometimes as long,

有时数字可以被java解释为int,有时也可以解释为long,

so to resolve the issue i use the 'toString()' function as follows:

所以为了解决这个问题,我使用了“toString()”函数,如下所示:

public Object run(Object... args) {

  final long uid = Long.valueOf(args[0].toString());

回答by CodeShadow

long value = Long.parseLong((String)request.getAttribute(""));

回答by javabeginner

I faced the same problem but while doing JSP coding. The above mentioned suggestions regarding Long and generics either did not work or did not fit into the code fragment.

我在进行 JSP 编码时遇到了同样的问题。上面提到的关于 Long 和泛型的建议要么不起作用,要么不适合代码片段。

I had to solve it like this(in JSP):

我不得不像这样解决它(在JSP中):

<%Object y=itr.next(); %>

and afterwards use my Object y like <%=y%> as we would use any other Java variable in scriptlet.

然后像 <%=y%> 一样使用我的对象 y,因为我们将在 scriptlet 中使用任何其他 Java 变量。

回答by efkan

Numberclass can be used for to overcome numeric data-type casting.

Number类可用于克服数字数据类型转换。

In this case the following code might be used:

在这种情况下,可能会使用以下代码:

long a = ((Number)itr.next()).longValue();



I've prepared the examples below:

Objecttolongexample - 1



我准备了以下示例:

Objectlong示例 - 1

// preparing the example variables
Long l = new Long("1416313200307");
Object o = l;

// Long casting from an object by using `Number` class
System.out.print(((Number) o).longValue() );

Console outputwould be:

控制台输出将是:

1416313200307



Objecttodoubleexample - 2



Objectdouble例子- 2

// preparing the example variables
double d = 0.11;
Object o = d;

// Double casting from an Object -that's a float number- by using `Number` class
System.out.print(((Number) o).doubleValue() + "\n");

Console outputwould be:

控制台输出将是:

0.11



Objecttodoubleexample - 3

Be careful about this simple mistake! If a float value is converted by using doubleValue()function, the first value might not be equal to final value.
As shown below 0.11!= 0.10999999940395355.



Objectdouble例子- 3

小心这个简单的错误!如果使用doubleValue()函数转换浮点值,则第一个值可能不等于最终值。
如下图0.11!= 0.10999999940395355

// preparing the example variables
float f = 0.11f;
Object o = f;

// Double casting from an Object -that's a float number- by using `Number` class
System.out.print(((Number) o).doubleValue() + "\n");

Console outputwould be:

控制台输出将是:

0.10999999940395355



Objecttofloatexample - 4



Objectfloat例子- 4

// preparing the example variables
double f = 0.11;
Object o = f;

// Double casting from an Object -that's a float number- by using `Number` class
System.out.print(((Number) o).floatValue() + "\n");

Console outputwould be:

控制台输出将是:

0.11