简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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