Arrays are containers of multiple values of same kind of data type (int, String, long, double, char and also objects of any class) and referred by a common name. An array is an object and can have fixed length of values in one variable. As I said, array is an object, we use new operator to create an array object. To declare an array, we use the square brackets and below is the general form -
int nums[];
And to create an array object, we use new operator, here is the syntax -
nums = new int[6];
We can integrate both statements into one statement -
int nums[] = new int[6];
The number in the square bracket represents the size / length of an array. The memory allocated for only number of elements, that array can hold, as determined by size. Then the reference to that memory is assigned to the array variable / object. So, an array is also called as a reference variable. Here below is the array of 6 int data type variables.
Each item is in array is called an element. And each element is accessed by numerical index starting from 0. Like in above example, to access the 5th element, you need to use the index number 4 in the square bracket like nums[4] and it will return its value as 15. As I mentioned earlier, array is an object. It has one data member / property length which tells the size of the array (total number of elements in an array) - nums.length
NOTE: Arrays start with index 0, means 1st element is - nums[0]
Arrays are used to store same kind of data in one variable and can be accessed by a common name. To assign values to an array, there are two ways. Either you assign values to each element separately like below -
nums[0]:11
nums[1]:3
nums[2]:7
nums[3]:8
nums[4]:20
nums[5]:16
NOTE: I have already covered for loops in my earlier posts. In above for loop, when printing I use i as an index and while printing I put + sign in println statement - it's called string concatenation. I will talk about it more later when discussing String class.
Index | 0 | 1 | 2 | 3 | 4 | 5 |
nums[] | 11 | 3 | 7 | 8 | 20 | 16 |
Each item is in array is called an element. And each element is accessed by numerical index starting from 0. Like in above example, to access the 5th element, you need to use the index number 4 in the square bracket like nums[4] and it will return its value as 15. As I mentioned earlier, array is an object. It has one data member / property length which tells the size of the array (total number of elements in an array) - nums.length
NOTE: Arrays start with index 0, means 1st element is - nums[0]
Arrays are used to store same kind of data in one variable and can be accessed by a common name. To assign values to an array, there are two ways. Either you assign values to each element separately like below -
int nums[] = new int[6]; nums[0] = 11; nums[1] = 3; nums[2] = 7; nums[3] = 8; nums[4] = 20; nums[5] = 16;Or you can assign the values in one line. The below statement automatically assigns the index to an array element and will be the same result as above.
int nums[] = {11, 3, 7, 8, 20, 16};You can access an element of an array individually using directly the index number in square brackets.
System.out.println("nums[0]:" + nums[0]); System.out.println("nums[1]:" + nums[1]); System.out.println("nums[2]:" + nums[2]); System.out.println("nums[3]:" + nums[3]); System.out.println("nums[4]:" + nums[4]); System.out.println("nums[5]:" + nums[5]);Or you can access all the elements going through an array using the for loop and array's size - length property (specifies how many times loop is going to run).
for(int i = 0; i < nums.length; i++) { System.out.println("nums[" + i + "]:" + nums[i]); }The result will be same like as below:
nums[0]:11
nums[1]:3
nums[2]:7
nums[3]:8
nums[4]:20
nums[5]:16
NOTE: I have already covered for loops in my earlier posts. In above for loop, when printing I use i as an index and while printing I put + sign in println statement - it's called string concatenation. I will talk about it more later when discussing String class.
No comments:
Post a Comment