Jump to content

Array length: Difference between revisions

J: flesh out the description
m (→‎{{header|J}}: clarification)
(J: flesh out the description)
Line 244:
 
=={{header|J}}==
Tally (<code>#</code>) returns the length of the leading dimension of an array (or 1 if the array has no dimensions). Shape Of (<code>$</code>) returns the length of each dimension of an array.
<lang j> # 'apple';'orange'
2
Line 250:
2</lang>
For the list array example given, the result appears to be the same. The difference is that the result of Tally is a scalar (array of 0 dimensions) whereas the result of Shape Of is a list (1 dimensional array), of length 1 in this case.
<lang j> $#'apple';'orange'
 
$$'apple';'orange'
1</lang>
This might be a clearer concept with a few more examples. Here's an array with two dimensions:
<lang j> >'apple';'orange'
apple
orange
$>'apple';'orange'
2 6
#>'apple';'orange'
2</lang>
And, here's an array with no dimensions:
<lang> 9001
9001
#9001
1
$9001
</lang>
You can count the number of dimensions of an array (the length of the list of lengths) using <code>#$array</code>:
<lang j>
#$9001
0
#$'apple';'orange'
1
#$>'apple';'orange'
2</lang>
 
=={{header|Java}}==
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.