Can we use a switch statement for integer arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we use a switch statement for integer arrays

I have an array list of integers. I want to use a switch to display it, is it possible?

29th Nov 2019, 2:35 AM
Mwebaze Andrew
Mwebaze Andrew - avatar
2 Answers
+ 7
If you want to switch on the array size or the value of an element, yes you can. To switch on something else, depends on your being able to make an integer of it. Anything you can turn into an integer, can be switched on so round of the mean, mode, or median of the list works. First step would be to create if statements to do what you want. If they all compare the same variable to one integer value, it can become a switch.
29th Nov 2019, 3:40 AM
John Wells
John Wells - avatar
+ 3
Mwebaze Andrew Yes it is possible, take the user input which should be an index present in the array list and pass it to the switch. If the index not present then in default case you can give as "index limit exceeded". Also use "trimToSize" command to shrink the size of the list so that it doesn't have empty spaces. Edit: Kindly let me know whether you are talking about arrays or array lists.
29th Nov 2019, 3:25 AM
Avinesh
Avinesh - avatar