简体   繁体   English

增加 Integer 的 int 值?

[英]Increment a Integer's int value?

How do I increment a Integer's value in Java?如何增加 Java 中的整数值? I know I can get the value with intValue, and I can set it with new Integer(int i).我知道我可以用 intValue 获取值,我可以用 new Integer(int i) 设置它。

playerID.intValue()++;

does not seem to work.似乎不起作用。

Note: PlayerID is a Integer that has been created with:注意:PlayerID 是 Integer,创建时使用:

Integer playerID = new Integer(1);

Integer objects are immutable, so you cannot modify the value once they have been created. Integer对象是不可变的,因此一旦创建它们就不能修改其值。 You will need to create a new Integer and replace the existing one.您将需要创建一个新的Integer并替换现有的Integer

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

As Grodriguez says, Integer objects are immutable.正如 Grodriguez 所说, Integer对象是不可变的。 The problem here is that you're trying to increment the int value of the player ID rather than the ID itself.这里的问题是您试图增加玩家 ID 的int值而不是 ID 本身。 In Java 5+, you can just write playerID++ .在 Java 5+ 中,您可以只编写playerID++

As a side note, never ever call Integer 's constructor.作为旁注,永远不要调用Integer的构造函数。 Take advantage of autoboxing by just assigning int s to Integer s directly, like Integer foo = 5 .通过直接将int s 分配给Integer s 来利用自动装箱,例如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.这将透明地使用Integer.valueOf(int) ,这优于构造函数,因为它并不总是必须创建一个新对象。

Java 7 and 8. Increment DOES change the reference, so it references to another Integer object. Java 7 和 8. Increment 确实会更改引用,因此它引用了另一个 Integer 对象。 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. Integer 本身仍然是不可变的。

AtomicInteger

Maybe this is of some worth also: there is a Java class called AtomicInteger .也许这也有一些价值:有一个名为AtomicInteger的 Java 类。

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.此类有一些有用的方法,例如addAndGet(int delta)incrementAndGet() (以及它们的对应方法),它们允许您递增/递减同一实例的值。 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.尽管该类旨在用于concurrency的上下文中,但它在其他场景中也非常有用,并且可能适合您的需要。

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

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 对象来保存结果:

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

For Java 7, increment operator '++' works on Integers.对于 Java 7,增量运算符“++”适用于整数。 Below is a tested example下面是一个经过测试的例子

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

Maybe you can try:也许你可以试试:

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

You can use IntHolder as mutable alternative to Integer.您可以使用 IntHolder 作为 Integer 的可变替代品。 But does it worth?但值得吗?

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++ ,真正发生的事情是这样的:

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

After Java 8, we can use as below Java 8之后,我们可以如下使用

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

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

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