Arrays?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Arrays??

anyone who understands the concept of arrays in details?

10th Oct 2016, 12:00 PM
EARNEST KIAMBI KINYUA
EARNEST KIAMBI KINYUA - avatar
2 Answers
+ 2
What do you need to know? An array is quite literally... an array. A group of values of the same data type. int arr[3] = { 1, 2, 3 }; Here we have declared a single-dimension array with three elements. int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; Herr we have declared a two-dimension array with 6 elements. You can have as many dimensions as you need, just know that the more dimensions you have, the more of a headache you're going to give yourself.
10th Oct 2016, 2:53 PM
Cohen Creber
Cohen Creber - avatar
+ 1
Say you are writing a contact list, and you are dealing three telephone numbers: string numberA = "555-555555555"; string numberB = "555-555555556"; string numberC = "555-555555557"; That works, you can now just write all your code in terms of these numbers. if(userSearchedFor(numberA)){ ... } if(userSearchedFor(numberB)){ ... } if(userSearchedFor(numberC)){ ... } As your app gets more complicated you might want to add a fourth number to your contact list, and chances are you have to rewrite lots of your code. Or what if you don't even know how many telephone numbers you will be storing? Maybe you want to add more, even after you have written your code. 'Ordinary variables' can only get you so far - at some point you need a list. And you can make the most basic of those (arrays) much like Cohen described. string numbers[3] = { "555-555555555", "555-555555556", "555-555555557" }; To use lists you need to know about loops, but once you get there it's much nicer to work with. We can use the subscript to pick out any number out of our array, for example numbers[0] == "555-555555555", numbers[1] == ""555-555555556", etc, and this makes it so we can use a for-loop to loop over all numbers (however many there may be!): for(var i = 0; i < 3; i++){ if(userSearchedFor(numbers[i]){ ... } }
10th Oct 2016, 4:01 PM
Schindlabua
Schindlabua - avatar