无法从类型对象转换为长整型

发布于 2024-08-22 21:27:51 字数 308 浏览 5 评论 0原文

我有一个名为 table 的哈希表。类型值为long。我使用 .values() 获取值。现在我想访问这些值。

Collection val = table.values();

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

但是当我尝试获取它时,它给了我错误,因为我无法从类型 object 转换为 long。我该如何绕过它?

I have a hashtable named table. The type value is long. I am getting values using .values(). Now I want to access these 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 object to long. How can I go around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

青衫负雪 2024-08-29 21:27:51

试试这个:

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

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

另一种选择是使用泛型:

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

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.

Another option is to use Generics:

  Iterator<Long> itr = val.iterator();
  Long a = itr.next();
失而复得 2024-08-29 21:27:51

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

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

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

我准备了以下示例:

对象 示例 - 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() );

控制台输出将是:

1416313200307

< br>
对象 double 示例 - 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");

控制台输出将是:

0.11

< br>
对象 示例 - 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");

控制台输出将是:

0.10999999940395355

对象 浮动 示例 - 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");

控制台输出将是:

0.11

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

In this case the following code might be used:

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

I've prepared the examples below:

Object to long example - 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 output would be:

1416313200307

Object to double example - 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 output would be:

0.11

Object to double example - 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.

// 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 output would be:

0.10999999940395355

Object to float example - 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 output would be:

0.11
幽梦紫曦~ 2024-08-29 21:27:51

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

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

帝王念 2024-08-29 21:27:51

您应该使用 Java 5 中的泛型功能。

当你从a中取出一个元素时
集合,您必须将其投射到
存储在的元素类型
收藏。 除了是
不方便,这样不安全
。这
编译器不会检查你的转换
与集合的类型相同,
因此转换可能会在运行时失败。

泛型为您提供了一种方法
传达集合的类型
到编译器,这样就可以
检查过。一旦编译器知道
集合的元素类型,
编译器可以检查你是否使用了
集合一致并且可以
插入正确的值转换
已从集合中删除。

您可以阅读此快速 howto 或此更完整的教程

You should use the new Generics features from 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 howto or this more complete tutorial.

奶茶白久 2024-08-29 21:27:51

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

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

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

public Object run(Object... args) {

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

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

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

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

public Object run(Object... args) {

  final long uid = Long.valueOf(args[0].toString());
゛清羽墨安 2024-08-29 21:27:51
long value = Long.parseLong((String)request.getAttribute(""));
long value = Long.parseLong((String)request.getAttribute(""));
一杯敬自由 2024-08-29 21:27:51

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

我必须像这样解决它(在 JSP 中):

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

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

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.

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文