什么是未初始化的项目之间在一个数组在JavaScript中和Java的区别? [英] What is the difference between non-initialized items in an array in JavaScript vs Java?

查看:126
本文介绍了什么是未初始化的项目之间在一个数组在JavaScript中和Java的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在玩弄JavaScript和卡住了一个简单的程序。

我宣布在JavaScript中的数组像

  VAR A = [0,1,2];

然后,当没有固定的大小为在JavaScript中的阵列,我们可以对阵列添加更多的,我加入到阵列另一整数

  A [3] = 3;

和预期如果我尝试访问 A [4] 我一定要得到它为未定义

现在,如果我拿一个数组

  VAR A = [0,1,2];

和添加其他元素

  A [4] = 4;

我故意没有定义 A [3] ,而这也给了我 A [3] 未定义

下面就是这可以观察到一个小提琴: http://jsfiddle.net/ZUrvM/

现在,如果我尝试在Java中同样的事情,

  INT [] A = INT新[4];
一个[0] = 0;
一个[1] = 1;一个[3] = 3;

然后我结束了

  A [2] = 0;

您可以看到这对ideone: https://ideone.com/WKn6Rf

这样做的原因在Java中,我发现的是,虽然宣称阵列的四个变量的定义,我们可以只将值分配给数组的声明的大小。
但是,在JavaScript中,当我宣布大小为3的数组,然后添加5个元素为何不考虑第四元素是 0 如果我们已经增加了数组的大小超过4?

为什么我看到在JavaScript中这种奇怪的行为,而不是在其他的语言吗?


解决方案

  

为什么在JavaScript中这种奇怪的行为?


由于数组是唯一的对象。如果你访问一个不存在的属性,你回来未定义。你根本没在首页 3 指定的元素,所以没有什么。

自动增长通过分配更高的指标不改变这种行为的阵列。它会影响到。长度属性,是的,但中间指数将保持不存在的。这被称为一个稀疏数组


  

为什么是Java这种奇怪的行为/ C / C ++?


由于数组是分配的内存大块,并且分配大小为4的数组时,它的所有元素从该内存位置取它们的值。为了避免不确定的值,在一些语言/场合领域得到默认初始化,通常与 0

I was just playing around with JavaScript and got stuck with a simple program.

I declared an array in JavaScript like

var a = [0, 1, 2];

Then as there is no fixed size for an array in JavaScript and we can add more to the array, I added another integer to array.

a[3] = 3;

And as expected If I try to access a[4] I am definitely going to get it as undefined.

Now, if I take an array

var a = [0,1,2];

And add another element

a[4] = 4;

I have intentionally not defined a[3], and this also gives me a[3] as undefined.

Here is a fiddle where this can be observed: http://jsfiddle.net/ZUrvM/

Now, if I try the same thing in Java,

int[] a = new int[4];
a[0] = 0;
a[1] = 1;

a[3] = 3;

Then I end up with

a[2] = 0;

You can see this on ideone: https://ideone.com/WKn6Rf

The reason for this in Java I found is that the four variables are defined while declaring the array and we can only assign values to the declared size of array. But in JavaScript when I declare an array of size 3 and then add 5th element why does it not consider the 4th element to be null or 0 if we have increased the array size beyond 4?

Why do I see this strange behavior in JavaScript, but not in other languages?

解决方案

Why is this strange behavior in JavaScript?

Because arrays are only objects. If you access a nonexisting property, you get back undefined. You simply didn't assign an element at index 3, so there is nothing.

Auto-growing the array by assigning higher indices does not change this behaviour. It will affect the .length property, yes, but the intermediate indices will stay nonexistent. This is known as a sparse array.

Why is this strange behaviour in Java / C / C++?

Because arrays are chunks of allocated memory, and when allocating an array of size 4, all its elements take their values from that memory location. To avoid indeterminate values, in some languages/occasions the fields get default-initialised, typically with 0.

这篇关于什么是未初始化的项目之间在一个数组在JavaScript中和Java的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆