在 JavaScript 中实现 Arraylist
我们非常熟悉 Java 中的 Arraylist
,它允许我们在不指定数组大小的情况下向数组添加元素。这是一个不错的功能,但我们在 JavaScript 中有类似的功能吗?每种语言都有自己的一组数据结构。在 JavaScript 中,我们有可以存储同构值和异构值的数组。异构意味着值可能是各种数据类型,如字符串、数字、布尔值等,都放在一个数组中。与 Java 中的 ArrayList
相比,它是一个独特且更好的特性。
在 JavaScript 中创建数组
很容易。我们使用 var
关键字创建一个数组。它类似于我们在 Java 中使用 ArrayList
创建数组的方式。Java 坚持指定 ArrayList
的数据类型。但是在 JavaScript 中,我们并没有明确声明 Array 的数据类型。我们让 JavaScript 解释器根据数组中分配的值做出决定。无论如何,对于 JavaScript,数组的类型是 object
。参考以下代码创建一个 Array。
var a = [1, 6, 3, 5];
console.log(a.length);
console.log(typeof a);
console.log(typeof a[2]);
输出:
4
object
number
就像 Java 中的 Arraylist
一样,我们可以使用循环、for
循环和 while
循环遍历 JavaScript 数组。JavaScript 还具有 .foreach()
函数,用于迭代数组中的元素。我们可以使用 foreach()
在每个 Array 元素上执行某些代码行。foreach
函数接受一个函数作为参数。我们可以将此函数编写为内联函数。检查下面的代码片段以更好地理解。
var a = ["hello", 1, false];
a.forEach((i) => {
console.log(typeof i);
})
输出:
string
number
boolean
在这里,我们对 Array 的每个元素应用 typeof
操作。该代码进一步将输出记录到 Web 浏览器控制台。JavaScript 有另一个函数,.reverse()
函数,它反转数组中元素的位置。参考以下代码。
var a = ["hello", 1, false];
console.log(a.reverse());
输出:
[false, 1, "hello"]
上述日志是在 Google Chrome 网络浏览器控制台中获得的。
我们可以使用 Java 中的 .add()
函数将元素添加到 ArrayList
。类似地,在 JavaScript 中,我们有一些函数可以用来在数组的不同位置添加元素。
var a = ["hello", 1, false];
a.push(2.15);
console.log(a);
a.unshift(123);
console.log(a);
输出:
["hello", 1, false, 2.15]
[123, "hello", 1, false, 2.15]
输出是在 google chrome 网络浏览器中获得的。请注意,这些操作无需使用赋值运算符即可更改原始数组。
同样,我们可以使用以下 JavaScript 函数从数组中删除元素。它类似于 Java 中 ArrayList
的 remove()
函数。
var a = [123, "hello", 1, false, 2.15];
a.pop();
console.log(a);
a.shift();
console.log(a);
输出:
[123, "hello", 1, false]
["hello", 1, false]
这里有几点需要注意。
JavaScript 中有另一种方法,splice()
函数。我们可以使用它从数组中添加或删除一个或多个元素。splice()
函数采用以下几个参数。
让我们看看下面的代码来了解更多。
var a = [123, "hello", 1, false, 2.15];
a.splice(2, 1, "there");
console.log(a);
a.splice(2, 2);
console.log(a);
a.splice(2);
console.log(a);
输出:
[123, "hello", "there", false, 2.15]
[123, "hello", 2.15]
[123, "hello"]
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。