Java object to int:更好的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/890162/
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
Java object to int: a better way?
提问by Jeremy
I have a TreeSet, which will be full of integers. To make a long story short, I'm trying to loop starting after the last (greatest) value stored in the list. What I'm doing now to get the starting variable is:
我有一个 TreeSet,它将充满整数。长话短说,我试图从存储在列表中的最后一个(最大)值之后开始循环。我现在正在做的获取起始变量是:
Object lastObj = primes.last();
Integer last = new Integer(lastObj.toString());
int start = 1 + last.intValue(); // the added 1 is just for program logic
I'm sure that there must be a better way to cast an object (which I know will always be an int) into the int 'start'. Anyone know of a better way of doing this?
我确信一定有更好的方法将一个对象(我知道它永远是一个 int)转换为 int 'start'。有人知道这样做的更好方法吗?
采纳答案by Vincent Ramdhanie
Are you using Java version 1.6? In that case you can take advantage of autoboxing and generics to clean up the code.
您使用的是 Java 1.6 版吗?在这种情况下,您可以利用自动装箱和泛型来清理代码。
First, the TreeSet can be declared as containing only Integer objects
首先,可以将 TreeSet 声明为仅包含 Integer 对象
TreeSet<Integer> primes;
Now to get the object from the set you can
现在要从集合中获取对象,您可以
Integer last = primes.last();
and using the autoboxing feature you get
并使用您获得的自动装箱功能
int start = 1 + last;
回答by jbm
In J2SE 5 or later it happens automatically, with the "autoboxing" feature.
在 J2SE 5 或更高版本中,它会自动发生,具有“自动装箱”功能。
int start = 1 + last;
http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
Also, if you know they're all going to be Integer objects, use the parameterized type:
此外,如果您知道它们都是 Integer 对象,请使用参数化类型:
List<Integer> obj = new ArrayList<Integer>()...
Then combine it with the autoboxing feature:
然后将它与自动装箱功能结合起来:
int start = 1 + obj.last();
回答by lostlogic
If you know that they were 'int' when they were put in, then they were transformed to Integer while in the Collection (Collections cannot contain primitives, only objects), as such, you can simply Integer last = (Integer)lastObj;
.
如果您知道它们在放入时是“int”,那么它们在集合中时会被转换为 Integer(集合不能包含基元,只能包含对象),因此,您可以简单地Integer last = (Integer)lastObj;
.
Ideally though, you would use a TreeSet<Integer>
and then it would just feed you Integers in the first place.
理想情况下,您会使用 aTreeSet<Integer>
然后它首先会为您提供整数。
回答by James McMahon
Post Java 1.4, you can use autoboxing.
在 Java 1.4 之后,您可以使用自动装箱。
So it becomes,
于是就变成了,
int start = 1 + (Integer) primes.last(); // the added 1 is just for program logic
If you used generics with your TreeSet (TreeSet<Integer>
) you could remove the initial cast to Integer.
如果您在 TreeSet ( TreeSet<Integer>
) 中使用泛型,您可以删除初始转换为 Integer。
回答by Ryan Emerle
Why can't you just cast it, rather that converting to a string, then parsing that string and creating a new reference?
为什么不能直接转换它,而是转换为字符串,然后解析该字符串并创建一个新的引用?
Object lastObj = primes.last();
int start = 1 + ( ( Integer )lastObj ).intValue();
回答by Zifre
If you know primes
is holding just integers, you should make primes
into a TreeSet<Integer>
.
如果你知道primes
是只持有整数,你应该primes
到TreeSet<Integer>
。
It would then become:
然后它会变成:
int start = 1 + primes.last().intValue();
If you can't use generics use this:
如果您不能使用泛型,请使用以下命令:
int start = 1 + ((Integer)prime.last()).intValue();
Casting into a string would just be silly.
转换成字符串只是愚蠢的。
By the way, I don't suggest using autoboxing. It does all kinds of things behind your back. Explicitly using Integer
seems clearer to me. But this is just my personal preference, you can use autoboxing if you want.
顺便说一下,我不建议使用自动装箱。它在你背后做各种各样的事情。显式使用Integer
对我来说似乎更清楚。但这只是我个人的喜好,如果你愿意,你可以使用自动装箱。