In java array why we are using .length to get numbers or to check?? Ex: i <arr.length | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In java array why we are using .length to get numbers or to check?? Ex: i <arr.length

29th Mar 2018, 2:47 PM
nithish kumar
nithish kumar - avatar
5 Answers
+ 3
Basically, i is your starting point at 0, and arr.length is the size/length of the array. You do this so that you can loop through each element of the array by starting at 0 and stopping once you reach the end of the array (.length). You increase i each loop through so that it references the next element. For example: for(int i = 0; i < arr.length; ++i) ^That's saying start the iteration (var i) at 0, stop the loop if it reaches the end of the array, and for each loop through increase variable 'i' by 1 so it can reference the next element of the array.
29th Mar 2018, 2:52 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 2
arr.length returns the size of the array (how many objects it can hold) e.g. int[] arr = new int[5]; System.out.println(arr.length); will print 5
29th Mar 2018, 2:50 PM
TurtleShell
TurtleShell - avatar
+ 2
@Nithish You don't -have- to use length toward those means, but sometimes you don't know what your array's length will be prior, so this is how you can ensure you're capturing the correct amount of iterations. As well, this gives it some form of an end point, rather than running endlessly. As an example, I could do this: for(int i = 0; i < 10; ++i) ^That doesn't use length as part of the condition. It'll stop once the variable 'i' reaches 10 iterations (0-9). You could do this also: while(true) ^That creates an infinite loop that'll keep going until you break the loop, typically through an IF condition somewhere inside of the loop. Imagine that it's an input box in a console, you could allow input to continue for the user until they type 'exit' or something like that. Inside the loop is an IF condition that checks for the input 'exit' and breaks the loop or exits the program. So to sum it up, using '.length' is not required, but it is very useful when dealing with arrays.
29th Mar 2018, 4:38 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
is that compulsory to use length?
29th Mar 2018, 3:06 PM
nithish kumar
nithish kumar - avatar
+ 1
Basically, an Array is an Object. .length is a mathod that returns the number of variable you have in your array. For exemple, in java you can declares an array using this form: int[] arr = new int[5] The above code means that you create an object that have 5 variables. For acess this variables you use a pointer [i]. The i is a value pointer. If i = 0, you'll acess your first variable. So, we have the method .length() for help us in our logic. An basic exemple using the method .length() is: for(int i =0; i<arr.length(); i++){system.out.println(arr[i].toString())} Basically the code above is an loop that start with the value of i=0 and stop when i = num of elements in your object array. In this way, you can access all the variables in your array.
29th Mar 2018, 3:47 PM
Jabel Tiago Martins
Jabel Tiago Martins - avatar