Why are arrays useful | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why are arrays useful

explain

20th May 2018, 7:01 AM
Sufyan Ali
Sufyan Ali - avatar
4 Answers
+ 8
If you want to display a lot of numbers. 1. System.out.println(1); System.out.println(2); System.out.println(3); 2. int[] arr={1,2,3}; for (int i:arr){ System.out.println(i);} Array is also easy to maintain👍
20th May 2018, 1:49 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
Array is collection of elements with the single data type which are stored in adjacent memory location. For example : if you want to store 50 value for some reason, you will have to declare 50 variables because a single variable can store only one value at a time. This will make the code long and complex. So in such a situation you need to declare variable that can store 50 values. This can be achieved using arrays
20th May 2018, 7:17 AM
Nova
Nova - avatar
+ 2
i agree wid Keval Dhanani
20th May 2018, 7:30 AM
Harsh Agrawal
Harsh Agrawal - avatar
+ 2
An example would be the states in a country. If you create an array of them it is a simple matter to loop through the array and generate the options for a select dropdown rather than hardcoding every single one of them. Especially if you have multiple of them on a site or a form. Then if you change the array in one place all of the dropdown do. Arrays save time and effort and keep code cleaner and more meaningful than x number of individual variables. Returns from a database query are stored as arrays as well because of how simple it is to navigate their structure inside of a loop.
20th May 2018, 9:31 AM
Adam
Adam - avatar