我一直发现这里和谷歌上的人都有从long到int的麻烦,而不是相反。然而,我确信我不是唯一一个在从int型转向Long型之前遇到这种情况的人。

我找到的唯一其他答案是“一开始就把它设置为长”,这并没有解决这个问题。

我最初尝试了类型转换,但我得到了一个“不能从int类型转换为Long类型”

for (int i = 0; i < myArrayList.size(); ++i ) {
    content = new Content();
    content.setDescription(myArrayList.get(i));
    content.setSequence((Long) i);
    session.save(content);
}

正如你可以想象的,我有点困惑,我被困在使用int,因为一些内容是作为一个数组列表进来的,而我存储这个信息的实体需要序列号作为一个长。


当前回答

use

new Long(your_integer);

or

Long.valueOf(your_integer);

其他回答

 1,new Long(intValue);
 2,Long.valueOf(intValue);

使用以下方法:Long.valueOf(int);。

除了这里建议的其他方法外,还可以尝试下面的代码。

(long)intValue

原始到原始。

use

new Long(your_integer);

or

Long.valueOf(your_integer);
 //Suppose you have int and you wan to convert it to Long
 int i=78;
 //convert to Long
 Long l=Long.valueOf(i)