[JAVA] Differences in the behavior of each LL language when the list index is skipped

It's a story of "so what", but I thought it was interesting that the behavior was different in each language.

JavaScript

var arr = [];
arr[100] = 'foo';
console.log(arr.length); //=> 101
console.log(arr[0]); //=> undefined
console.log(arr[101]); //=> undefined

Undefined indexes return ʻundefined`.

Declaring " use strict "; does not change the execution result.

Perl

my @arr;
$arr[100] = 'foo';
print $#arr; #=> 100
print defined $arr[0] ? 1 : 0; #=> 0
print defined $arr[101] ? 1 : 0; #=> 0

Like JavaScript, undefined index seems to be ʻundef`.

Declaring ʻuse strict;` does not change the execution result.

Ruby

arr = []
arr[100] = 'foo'
p arr.size #=> 101
p arr[0] #=> nil
p arr[101] #=> nil

It's about the same as JavaScript / Perl, but it's nil. Probably because there is no equivalent of ʻundefined`.

PHP

$arr = array();
$arr[100] = 'foo';
echo count($arr); //=> 1
echo is_null($arr[0]); //=> 1
echo is_null($arr[101]); //=> 1

The undefined index will be null.

The length of the array is 1, which is different from other languages. This seems to be the case in PHP because both lists and hashes (dictionaries) are ʻarray`.

[Addition]

Access to an undefined index will result in a ʻE_NOTICE` level error. (See comments for details)

error_reporting(E_ALL); // E_Set to output NOTICE

$arr = array();
$arr[100] = 'foo';
echo $arr[0]; //=> Notice: Undefined offset: 0

Python

arr = []
arr[100] = 'foo' #=> IndexError: list assignment index out of range

If you try to reference / assign to an index that does not exist, you will get ʻIndexError`. I feel that this is the most straightforward behavior.

Recommended Posts

Differences in the behavior of each LL language when the list index is skipped
The story that inheritance behavior is quite different in each language
Behavior when SIGEV_THREAD is set in sigev_notify of sigevent with timer_create (C language)
Behavior in each language when coroutines are reused with for
Get the number of occurrences for each element in the list
Get the index of each element of the confusion matrix in Python
Behavior when returning in the with block
[GO language] Organize the behavior of import in places other than GOPATH
When the target is Ubuntu 16.04 in Ansible
List of language codes used in twitter (including API) (with Python dictionary). What is the most commonly used language?
[pandas] When specifying the default Index label in the at method, "" is not required
In Python, change the behavior of the method depending on how it is called
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
[Python] Sort the list of pathlib.Path in natural sort
Match the distribution of each group in Python
Make a copy of the list in Python
Behavior when Trainable = False of Container in Keras
About the behavior of Model.get_or_create () of peewee in Python
Search by the value of the instance in the list
[Solution] When inserting "0001" into the column of string in sqlite3, it is entered as "1".
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
When the selected object in bpy.context.selected_objects is not returned
[python] Get the list of classes defined in the module
Check if it is Unix in the scripting language
Embedding in datetime when only the time is known
[Python] Outputs all combinations of elements in the list
Check if it is Unix in the scripting language
When you think the update of ManjaroLinux is strange
Creating a list when the nomenclature is a fixed time
Get the value of a specific key up to the specified index in the dictionary list in Python
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[Natural language processing] I tried to visualize the remarks of each member in the Slack community
A note on the default behavior of collate_fn in PyTorch
Check the operation of Python for .NET in each environment
Behavior when multiple servers are specified in nameservers of dnspython
Get the number of specific elements in a python list
Get index of nth largest / smallest value in list in Python
Find out how many each character is in the string.
Numerical approximation method when the calculation of the derivative is troublesome
Get index of nth largest / smallest value in list in Python
Explanation of CSV and implementation example in each programming language
Precautions when drawing the probability density function and the histogram on top of each other in matplotlib