草庐IT

random_integer

全部标签

java - Integer.parseInt(string) 实际上是如何工作的?

最近被问到这个问题,不知道答案。有人可以从高层次上解释Java如何获取字符/字符串并将其转换为int。 最佳答案 通常是这样完成的:初始化结果为0对字符串中的每个字符执行此操作结果=结果*10从字符中获取数字('0'是48ASCII(或0x30),因此只需从字符ASCII码中减去它即可得到数字)将数字添加到结果中返回结果编辑:如果您将10替换为正确的基数并调整从相应字符获取数字的方式,这适用于任何基数(对于低于10的基数应该正常工作,但需要稍加调整以适应更高的基数-例如十六进制-因为字母与数字相隔7个字符)。编辑2:字符到数字值的转

Java integer++i 不改变值

我刚刚做了这个简单的“程序”:publicstaticvoidmain(String[]args){inti=1;intk=0;while(true){if(++i==0)System.out.println("loop:"+++k);}}运行这个程序后,我立即得到输出:(...)loop:881452loop:881453loop:881454loop:881455loop:881456loop:881457loop:881458(...)好像i总是0。事实上,当我在Eclipse中调试时,在暂停程序时,i将始终为零。单步执行循环时,i会递增,但在恢复和挂起调试器时,i再次为0。当我

Java integer++i 不改变值

我刚刚做了这个简单的“程序”:publicstaticvoidmain(String[]args){inti=1;intk=0;while(true){if(++i==0)System.out.println("loop:"+++k);}}运行这个程序后,我立即得到输出:(...)loop:881452loop:881453loop:881454loop:881455loop:881456loop:881457loop:881458(...)好像i总是0。事实上,当我在Eclipse中调试时,在暂停程序时,i将始终为零。单步执行循环时,i会递增,但在恢复和挂起调试器时,i再次为0。当我

java - ArrayList<Integer> 接受字符串

publicclassMain{publicstaticvoidmain(String[]args){ArrayListar=newArrayList();Listl=newArrayList();l.add("a");l.add("b");ar.addAll(l);System.out.println(ar);}}输出:[a,b]不能直接加String至ArrayListar,但通过使用addAll()这是可能的。如何添加String至ArrayList其类型已被指定为Integer?任何人都可以突出明确的实现细节及其背后的原因吗? 最佳答案

java - ArrayList<Integer> 接受字符串

publicclassMain{publicstaticvoidmain(String[]args){ArrayListar=newArrayList();Listl=newArrayList();l.add("a");l.add("b");ar.addAll(l);System.out.println(ar);}}输出:[a,b]不能直接加String至ArrayListar,但通过使用addAll()这是可能的。如何添加String至ArrayList其类型已被指定为Integer?任何人都可以突出明确的实现细节及其背后的原因吗? 最佳答案

java - 为什么 notifyAll() 在 Integer 上同步时会引发 IllegalMonitorStateException?

为什么这个测试程序会导致java.lang.IllegalMonitorStateException?publicclasstest{staticIntegerfoo=newInteger(1);publicstaticvoidmain(String[]args){synchronized(foo){foo++;foo.notifyAll();}System.err.println("Success");}}结果:Exceptioninthread"main"java.lang.IllegalMonitorStateExceptionatjava.lang.Object.notifyA

java - 为什么 notifyAll() 在 Integer 上同步时会引发 IllegalMonitorStateException?

为什么这个测试程序会导致java.lang.IllegalMonitorStateException?publicclasstest{staticIntegerfoo=newInteger(1);publicstaticvoidmain(String[]args){synchronized(foo){foo++;foo.notifyAll();}System.err.println("Success");}}结果:Exceptioninthread"main"java.lang.IllegalMonitorStateExceptionatjava.lang.Object.notifyA

java - List<?> 是 List<Integer> 和 List<Number> 的共同父代吗?

来自thisOracletutorial,AlthoughIntegerisasubtypeofNumber,ListisnotasubtypeofListand,infact,thesetwotypesarenotrelated.ThecommonparentofListandListisList.我的问题是关于第二句话。怎么说List是List的共同parent和List??代表未知类型,可以是any引用类型。即使我这么说?将是Object在这里,Object成为Integer的共同parent和Number并不意味着List成为List的共同parent和List.

java - List<?> 是 List<Integer> 和 List<Number> 的共同父代吗?

来自thisOracletutorial,AlthoughIntegerisasubtypeofNumber,ListisnotasubtypeofListand,infact,thesetwotypesarenotrelated.ThecommonparentofListandListisList.我的问题是关于第二句话。怎么说List是List的共同parent和List??代表未知类型,可以是any引用类型。即使我这么说?将是Object在这里,Object成为Integer的共同parent和Number并不意味着List成为List的共同parent和List.

java - java.util.Random 有多好?

两个问题:对于我投入其中的每颗种子,我会得到不同的数字序列吗?有一些“死”的种子吗?(产生零或快速重复的那些。)顺便问一下,如果有的话,我应该使用哪些其他PRNG?解决方案:因为我将使用PRNG来制作游戏,所以我不需要它是加密安全的。我会选择MersenneTwister,因为它的速度和周期长。 最佳答案 在某种程度上,随机数生成器是类(class)的马。Random类使用合理选择的参数实现LCG。但它仍然表现出以下特点:相当短的时间(2^48)位的随机性并不相同(请参阅我关于randomnessofbitpositions的文章)