Question:
In Java array indices?
Heidi B
2008-06-01 16:50:50 UTC
In Java array indices, that is subscript values, must be:
A. an integer type.
B. non-negative.
C. positive.
D. less than or equal to the declared size of the array.
Three answers:
2008-06-01 18:20:31 UTC
A is probably true because an array index must be a whole number. I said "probably" because maybe it could also be a short integer type or long integer type, so it doesn't have to be only an integer type.



B is true. An array index can be zero or a positive whole number.



C is false because the first element of an array has the index zero.



D is false because although Java syntax allows you to use an arry index equal to or greater than the size of an array, these indices would refer to a memory location outside that allocated for the array. You would be committing a logical error. For example, if you declare (allocate memory for) an array of five elements, since [0] refers to the 1st element, [5] refers to a 6th element. But there are only five elements.
jaden404
2008-06-01 18:16:15 UTC
I am not sure what kind of question this is meant to be, as B and C are the same thing. A non-negative number is a positive number (unless you do not consider 0 to be positive).



At any rate, all of these answers are correct.
rudel
2016-10-22 12:02:37 UTC
arrays are static, so once you declare them, they have a series length. as an example, preserving int[one hundred] numbers; signifies that you've an array referred to as numbers that ought to save one hundred aspects of type integer. you won't be able to replace the shape of aspects an array can save once you declared it so, you are able to save numbers[0] = a million; numbers[a million] = 2; and so on once you're saying myArray[0], you're with reference to the first aspect of the array. in case you declared myArray as myArray[0], you've an array of 0 aspect (almost ineffective) EDIT: I see, so that you want to label myArray[0]? properly, couldn't you purely do an if actuality with reference to the priority the position if the block > 0 and that i == 0 the position myArray[i], println("a million");


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...