Java - Choices: Assume that the intData array has been declared and initialized, complete the following code to retrieve the last value of the array. int lastValue = __________________;
intData[intData.length] intData[intData.length - 1] intData[intData.length() - 1] intData[intData.length()]
@woodrow73
accessing the number of elements in an array is a bit tricky to start -- this is because you access the length without using parenthesis-- which means it isn't really calling a method.. it's probably accessing a field variable. So (intData.length == numberElementsInIntDataVariable) Remember that doing ``` int[] i = new int[2]; ``` will create-- i[0], i[1]. not i[2]. so currently; ``` int elementCount = i.length; ``` will return 2.
Unlike the String class's length method - ie; ``` String s = "Hello World"; int sLength = s.length(); //sLength == 11 ```
If int intData[] is an array, then to get the element of the array you use: intData[1]. This gives the output of 2nd element. To get the last element, you use int lastValue = intData[intData.length-1]
``` char[] letters = {'a', 'b', 'c'}; System.out.print(letters[0]); // Prints 'a' System.out.print(letters[1]); // Prints 'b' System.out.print(letters[2]); // Prints 'c' System.out.print(letters.length); // Prints 3 ```
Join our real-time social learning platform and learn together with your friends!