1

So my understanding is this...

String[] banks = {"B of A", "Chase", "Wells Fargo"};

Index: used as a reference to the actual place holder in the element's value of an array.

index 0 is "B of A"

index 1 is "Chase"

index 2 is "Wells Fargo"

Element: used to define how many values there are in an array (not including 0). In other words, Index == Element - 1 For example, this array has 3 elements:

element 1 is "B of A"

element 2 is "Chase"

element 3 is "Wells Fargo"

Values: used to define what the actual variable type value is being held in the elements/indexes

the value of index 0 and element 1 is "B of A"

the value of index 1 and element 2 is "Chase"

the value of index 2 and element 3 is "Wells Fargo"

Is this a correct interpretation? I want to make sure I'm learning this correctly.


My reason for asking, is to validate the question I got wrong on a test:

enter image description here

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • 1
    Your issue stems from numbering elements differently to the index. Element numbering starts at 0 too. Element 0 is at index 0. "Index" describes the storage location. "Element" describes the contents of a location. "Value" describes the content of an element. – Tibrogargan Nov 09 '16 at 00:41
  • hmmm so you are saying `index == element` and not `index == element - 1`? – Fiddle Freak Nov 09 '16 at 00:44
  • @FiddleFreak this is just semantics: an array with 5 elements will access them using indexes 0 to 4. There is not much more to it... – assylias Nov 09 '16 at 00:46
  • Not quite, an array is just a bunch of boxes. Which box is determined by an index. This is independent of what is in the box. The contents of a box is an element. Because array indexing in Java is zero based and it would be even more confusing to have two different numbering schemes for the same thing it's common practice to refer to "the element at index 5" as "element 5" or "the 5th element" or "Milla Jovovich" – Tibrogargan Nov 09 '16 at 00:46
  • @assylias That is why I think `index == element - 1`, right? – Fiddle Freak Nov 09 '16 at 00:47
  • When you are using java, you use "zero-based indexing" where the first element in an array is indexed by 0. – Munib Nov 09 '16 at 00:48
  • so would the correct answer have been `System.out.print(ary[800])`? – Fiddle Freak Nov 09 '16 at 00:49
  • Interesting... So terminology-wise, `index == element`. – Fiddle Freak Nov 09 '16 at 00:50
  • @FiddleFreak no. index just refers to a location, not the contents of the location. "element = array[index]" – Tibrogargan Nov 09 '16 at 00:51
  • @FiddleFreak in your example, to print the 800th element, you use ary[799]. – assylias Nov 09 '16 at 00:51
  • I thought `value = array[index]` and not `element = array[index]` – Fiddle Freak Nov 09 '16 at 00:51
  • @Tibrogargan So why did the OP get the answer wrong? – Scary Wombat Nov 09 '16 at 00:51
  • The first element is `ary[0]` the last element is `ary[799]` so the 800th element must be `ary[799]` but may also be referred to as `element 799` – Scary Wombat Nov 09 '16 at 00:52
  • @ScaryWombat This is why I kept saying I thought `index == element - 1` – Fiddle Freak Nov 09 '16 at 00:54
  • @ScaryWombat because "element 800" is short for "the element at index 800" which is "ary[800]". The confusion stems from people's inate sense that positions start at "1". There is no "0th" in english. – Tibrogargan Nov 09 '16 at 00:55
  • @Tibrogargan so there is no correct answer as it would result in a OOB exception? – Scary Wombat Nov 09 '16 at 00:56
  • @ScaryWombat you're conflating the answer from a) with b). (i.e. The assumption that `ary` in b) is the same `ary` declared as the answer to a) is incorrect). The question is ambiguous. – Tibrogargan Nov 09 '16 at 00:59
  • @Tibrogargan As it is question 1a and 1b and the variable is the same I would say that is a normal reaction. But anyway, SO is not really the platform for this discussion. – Scary Wombat Nov 09 '16 at 01:01
  • Think of it this way... If I were to ask you to give me the element one in the array. Would you give me `ary[0]` or `ary[1]`? – Fiddle Freak Nov 09 '16 at 01:45
  • So did you ever challenge your teacher on this? – eaglei22 Jul 11 '18 at 16:37
  • 1
    @eaglei22 I ended up being kicked from the course (to which all other students saw no reason for this and also left the course feeling it was unjust). It was with a university through edx. I instead just studied from a Java book and am much more comfortable with Java now ^^. – Fiddle Freak Aug 31 '18 at 03:54
  • @FiddleFreak, in my past experience in college (undergrad and graduate), it seems you come across once in a while a professor/instructor who nailed a teaching job right when they graduated. So I think the lack of real world experience can surface issues like these. Having used arrays enough, I would have immediately said give me the value of the array at index ___. I don't think I would have said element 800. But it sounds like it was trying to say ary[800]. But seeing how you just declared an array of size 800 in the previous question, it's only proper to assume 799, to get the 800th value. – eaglei22 Oct 02 '18 at 14:52

2 Answers2

3

Yes that is correct. You can also call the values keys, for example in swift.

Here is a good explanation too.

PHP: is there difference between Index, Element, Key, Value of an Array?... are they the same thing?

Community
  • 1
  • 1
Munib
  • 957
  • 1
  • 14
  • 30
  • 1
    I just wanted to make sure, because I got a question wrong on a test when I was actually right. Thanks. – Fiddle Freak Nov 09 '16 at 00:34
  • 1
    You can always show your teacher/prof stackoverflow. And with the edit you made, I strongly believe that your answer is correct. – Munib Nov 09 '16 at 00:38
  • @FiddleFreak Actually, you were wrong, but just about numbering. Element 0 is at index 0. – Tibrogargan Nov 09 '16 at 00:42
  • I see, so when someone says to you "hey I need an int array called ary with 800 elements", you give them `int[] ary = new int[799];`? – Fiddle Freak Nov 09 '16 at 01:05
  • No, you will give them `int[] ary = new int[800];`. Because when you use the above syntax, you are instantiating the array. Not accessing it, so with instantiation, you will use the number of elements when creating the said array. Accessing the 800th element in the array will simply by `ary[799];`. – Munib Nov 09 '16 at 01:08
  • "Accessing the 800th element in the array will simply by `ary[799];`". So the question was: "`print the value of element 800`". So my answer should have been correct then? – Fiddle Freak Nov 09 '16 at 01:14
  • Yes, it should be very much correct. No doubt about that. Keep us updated as to what happens. Would like to hear the teacher's explanation. Would you mind telling me which grade class this is, hs, uni? Thanks – Munib Nov 09 '16 at 04:45
  • PurdueX: CS180.2x AP Computer Science A: Java Programming Loops and Data Structures | purdue university | taking the course on edx. I usually make posts correcting 1/3rd of the material released every week. I just wanted to be sure I was correct here, before making this new post. I will await their answer. Thanks again... – Fiddle Freak Nov 09 '16 at 06:11
  • Got a response "Thanks for bringing that to our notice! Yes, by element 800 we mean the last element possible that is stored in the array. I've updated questions b and c to reflect that." – Fiddle Freak Nov 10 '16 at 18:16
  • @fiddleFreak Nice! Thats sick! – Munib Nov 10 '16 at 21:36
0

Honestly, it could go either way. Logically, you are correct. However, in order to avoid confusion (when in reality it caused even more), the convention is just to refer to the element at index 0 as element 0; even though, in reality, that is the 1st element. So your logic is correct, it’s just that convention has shifted away from logic over time. In reality the question is poorly worded since you can ask the same question but without the added confusion (by asking for the index of an element), so try to avoid the type of wording your question used as much as possible. If there are some cases where you can’t, make sure to clarify yours or the other person’s meaning ahead of time.