增加 Integer 的 int 值?

发布于 2024-09-25 10:17:51 字数 238 浏览 7 评论 0原文

如何在 Java 中增加整数的值?我知道我可以使用 intValue 获取值,并且可以使用 new Integer(int i) 设置它。

playerID.intValue()++;

似乎不起作用。

注意:PlayerID 是一个使用以下命令创建的整数:

Integer playerID = new Integer(1);

How do I increment a Integer's value in Java? I know I can get the value with intValue, and I can set it with new Integer(int i).

playerID.intValue()++;

does not seem to work.

Note: PlayerID is a Integer that has been created with:

Integer playerID = new Integer(1);

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

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

发布评论

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

评论(10

战皆罪 2024-10-02 10:17:51

Integer 对象是不可变的,因此一旦创建它们就无法修改其值。您需要创建一个新的Integer并替换现有的。

playerID = new Integer(playerID.intValue() + 1);

Integer objects are immutable, so you cannot modify the value once they have been created. You will need to create a new Integer and replace the existing one.

playerID = new Integer(playerID.intValue() + 1);
恋竹姑娘 2024-10-02 10:17:51

正如 Grodriguez 所说,Integer 对象是不可变的。这里的问题是您试图增加玩家 ID 的 int 值而不是 ID 本身。在 Java 5+ 中,您可以只编写 playerID++

附带说明一下,永远不要调用 Integer 的构造函数。通过直接将 int 分配给 Integer 来利用自动装箱,例如 Integer foo = 5。这将透明地使用 Integer.valueOf(int) ,这优于构造函数,因为它并不总是需要创建新对象。

As Grodriguez says, Integer objects are immutable. The problem here is that you're trying to increment the int value of the player ID rather than the ID itself. In Java 5+, you can just write playerID++.

As a side note, never ever call Integer's constructor. Take advantage of autoboxing by just assigning ints to Integers directly, like Integer foo = 5. This will use Integer.valueOf(int) transparently, which is superior to the constructor because it doesn't always have to create a new object.

我只土不豪 2024-10-02 10:17:51

AtomicInteger

也许这也有一些价值:有一个名为 AtomicInteger

这个类有一些有用的方法,例如 addAndGet(int delta)incrementAndGet() (及其对应项)它允许您增加/减少同一实例的值。尽管该类设计为在并发上下文中使用,但它也非常有用在其他场景中,可能适合您的需要。

final AtomicInteger count = new AtomicInteger( 0 ) ;
…
count.incrementAndGet();  // Ignoring the return value. 

AtomicInteger

Maybe this is of some worth also: there is a Java class called AtomicInteger.

This class has some useful methods like addAndGet(int delta) or incrementAndGet() (and their counterparts) which allow you to increment/decrement the value of the same instance. Though the class is designed to be used in the context of concurrency, it's also quite useful in other scenarios and probably fits your need.

final AtomicInteger count = new AtomicInteger( 0 ) ;
…
count.incrementAndGet();  // Ignoring the return value. 
久光 2024-10-02 10:17:51

Java 7 和 8。增量确实会更改引用,因此它引用另一个 Integer 对象。看:

@Test
public void incInteger()
{
    Integer i = 5;
    Integer iOrig = i;
    ++i; // Same as i = i + 1;
    Assert.assertEquals(6, i.intValue());
    Assert.assertNotEquals(iOrig, i);
}

整数本身仍然是不可变的。

Java 7 and 8. Increment DOES change the reference, so it references to another Integer object. Look:

@Test
public void incInteger()
{
    Integer i = 5;
    Integer iOrig = i;
    ++i; // Same as i = i + 1;
    Assert.assertEquals(6, i.intValue());
    Assert.assertNotEquals(iOrig, i);
}

Integer by itself is still immutable.

氛圍 2024-10-02 10:17:51

整数对象是不可变的。您无法更改对象本身保存的整数值,但可以创建一个新的 Integer 对象来保存结果:

Integer start = new Integer(5);
Integer end = start + 5; // end == 10;

Integer objects are immutable. You can't change the value of the integer held by the object itself, but you can just create a new Integer object to hold the result:

Integer start = new Integer(5);
Integer end = start + 5; // end == 10;
情栀口红 2024-10-02 10:17:51

对于 Java 7,增量运算符“++”适用于整数。下面是一个测试过的例子

    Integer i = new Integer( 12 );
    System.out.println(i); //12
    i = i++;
    System.out.println(i); //13

For Java 7, increment operator '++' works on Integers. Below is a tested example

    Integer i = new Integer( 12 );
    System.out.println(i); //12
    i = i++;
    System.out.println(i); //13
善良天后 2024-10-02 10:17:51

也许你可以尝试:

final AtomicInteger i = new AtomicInteger(0);
i.set(1);
i.get();

Maybe you can try:

final AtomicInteger i = new AtomicInteger(0);
i.set(1);
i.get();
梦中楼上月下 2024-10-02 10:17:51

您可以使用 IntHolder 作为 Integer 的可变替代品。但这值得吗?

You can use IntHolder as mutable alternative to Integer. But does it worth?

浪漫人生路 2024-10-02 10:17:51

所有原始包装对象都是不可变的。

我可能迟到了,但我想补充并澄清,当您执行 playerID++ 时,真正发生的事情是这样的:

playerID = Integer.valueOf( playerID.intValue() + 1);

Integer.valueOf(int) 将始终缓存范围 -128 到 127(含)内的值,并且可能缓存此范围之外的其他值。

All the primitive wrapper objects are immutable.

I'm maybe late to the question but I want to add and clarify that when you do playerID++, what really happens is something like this:

playerID = Integer.valueOf( playerID.intValue() + 1);

Integer.valueOf(int) will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

冰雪梦之恋 2024-10-02 10:17:51

Java 8之后,我们可以使用如下

import java.lang.Math;
Math.incrementExact(playerID);
//it increment the integer value to + 1. It is also available for long

After Java 8, we can use as below

import java.lang.Math;
Math.incrementExact(playerID);
//it increment the integer value to + 1. It is also available for long
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文