What are the differences between java arrays and javascript arrays?

青灯夜游
Release: 2023-01-07 11:43:48
Original
2553 people have browsed it

Difference: 1. The length of an array must be specified in Java and cannot be changed; while the length of a JavaScript array is variable and there is no mandatory requirement to define the length. 2. The data types in Java arrays must be consistent; JavaScript arrays can store any data type.

What are the differences between java arrays and javascript arrays?

The operating environment of this tutorial: windows7 system, javascript1.8.5&&java8 version, Dell G3 computer.

The difference between java array and javascript array

The length must be specified when defining an array in java, and the length cannot be changed after it is determined. Arrays in js are similar to collections in java. The length is variable and there is no mandatory requirement to define the length.

  • js array can be automatically expanded, and the array will not cross the boundary

  • JS array can store any data type

  • Once the length of a java array is defined, it cannot be changed

  • The data types in the java array must be consistent

javadefine array

int a[] = new int[3];//定义一个新数组,在内存中分配了3*4个字节
		//快速初始化数组的两种方式
		int b[] = {1,2,3};
		int c[] = new int[]{1,2,3};
Copy after login

javascriptdefine array

There are four ways in total

/*  
                数组:  
                1、数组内可以存放任意类型的数据  
                2、数组元素不赋值,则为undefined  
                3、打印数组时,如果某个元素没有赋值,则为“”  
                4、访问数组范围以外的元素时,不会出现越界异常,为undefined  
                5、定义的数组大小,依然可以添加更多的元素  
*/  
		var a = new Array();
		alert(a[5]);//undefined,不会出现下标越界 
		
		var b = new Array(5); //规定数组的长度为5
		b[7] = "ccc";
		alert(b[7]);//ccc,定义数组的长度没什么卵用
		
		var c = new Array("tom","lily","lucy");
		
		var d = ["tom","lily","lucy"];//推荐使用
Copy after login

[Recommended learning: javascript advanced Tutorial

The above is the detailed content of What are the differences between java arrays and javascript arrays?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!