+ 1
Why are arrays useful
explain
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đ
+ 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
+ 2
i agree wid Keval Dhanani
+ 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.