nus-cs2030 / 2021-s2

2 stars 2 forks source link

"..." in method signatures #106

Open brennanleez-coder opened 3 years ago

brennanleez-coder commented 3 years ago

Hi everyone, first of all shoutout to my fellow GitHubbers, jettevy, keithlimhs, gwajoon, wentinggy, hope yall are doing fine. Today my TA spoke about Var Args in the Stream.of method API and i thought i would share it with you guys.

Screenshot 2021-03-26 at 1 24 12 PM

The "..." after type T is called VarArgs if yall ever wondered and basically you can treat values as an array. VarArgs means that you can input any amount of variables, for example (int... n), you can input as many integers as you want. Taking my jshell test as an example,

Screenshot 2021-03-26 at 1 32 22 PM

it can be seen when you input (1,2,3,4,5) into the method sum, it sums all of them up. you can also input (1,2,3,4,5,6) and it will sum all of it up. As the name suggests, it can take in varying amounts of arguments.

Do also take note that any VarArgs parameter should be the last, if not it will not compile.

Screenshot 2021-03-26 at 1 33 36 PM

This is due to the method not knowing how many inputs there is for the first or second VarArgs.

wentinggy commented 3 years ago

well written, thank you for sharing with us your knowledge! Really appreciate it ^^

wentinggy commented 3 years ago

can't wait to use it in the upcoming levels of the project!

fwenyin commented 3 years ago

thank you for clarifying!

itsjiaming commented 3 years ago

Thanks for sharing, brennanleez-coder! With this knowledge, I can now confidently tackle Lab 6!

Keithlimhs commented 3 years ago

This issue has indeed been very insightful! Thank you brennanleez-coder for expanding my knowledge and opening my mind! I feel extremely enlightened now and more confident of tackling my Lab 6!

KevinTan1203 commented 3 years ago

Just curious, does it work for other primitives like double, string objects etc?

eryuanren commented 3 years ago

@KevinTan1203 It actually works with any datatypes, primitives included

0xwuchuan commented 3 years ago

thank you this was very helpful!

kaiyuuu commented 3 years ago

Thanks for sharing this! I was very confused with the argument type but now I understand lab 6 better!

naozitios commented 3 years ago

thanks for the insight brennanenleecoderz

rendao1010 commented 3 years ago

well written! truly insightful piece of contribution. thank you!

lozhaowei commented 3 years ago

Thank you so much for the explanation! You quelled all the fears I had for this module!

peguin40 commented 3 years ago

Thank you for the clarification!