[英]Java initializing an integer and adding it to a list in one line
A tiny annoyance I have in my code right now - these two lines of code: 我现在在代码中有点烦恼-这两行代码:
int i = 1;
listOfIntegers.add(i);
I would really like these to be one line but I can't for the life of me find anything like "new int[] {1}" or similar. 我真的很想将这些作为一行,但是我一生无法找到类似“ new int [] {1}”之类的东西。 Is this possible?
这可能吗?
Yes, it is, and it is as easy as possible :) 是的,它是这样,并且它尽可能简单:)
listOfIntegers.add(1);
PS : For primitive data types you dont have to create "instances" of them, they are instances itself. PS:对于原始数据类型,您不必创建它们的“实例”,它们本身就是实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.