[Answered by Dayve and Andrew Grider] Index explained better please arr[2] = 42; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[Answered by Dayve and Andrew Grider] Index explained better please arr[2] = 42;

I do not understand what the Index is that number inside the brackets. EDIT: So would i be right in assuming this is like a Var the only real difference is that we can keep the Var name for multiple values by using index? EDIT 2: Ahhhh Andrew that made perfect sense to me it is basically like a bookshilf or a Data base containing under 1 variable multiple assigned values.

16th Jul 2017, 2:50 PM
Reisi
Reisi - avatar
2 Answers
+ 15
/* Starting from the scratch. Let's declare an Array named arr of type int with a maximum number of elements 10 : */ int arr[] = new int[10]; // Array Declaration /* Now, we need to add some elements into our arr as it has only the default value 0 at the moment. */ arr[2] = 42; /* Here, we added 42 to the array index 2 (remember that array index starts from 0) and it becomes the third element of our arr. If we try to print it's value, we get 42 as an output */ System.out.print(arr[2]); // outputs 42 This way, we can put any type of number into an array at any of its index :]
15th Jul 2017, 5:51 PM
Dev
Dev - avatar
+ 11
2 represents the index number it means the 3rd array of arr contains the value 42 array index numbers starts from 0,1,2...
15th Jul 2017, 5:46 PM
khadeeja Shamna
khadeeja Shamna - avatar