在 Java 的构造函数中初始化数组
本文介绍了如何在 Java 中的构造函数中初始化数组,并列出了一些示例代码来理解该主题。
数组是一种基于索引的数据结构,用于存储相似类型的数据。在 Java 中,我们可以使用数组来存储原始值和对象值。数组也是 Java 中的对象,并使用默认值进行初始化。例如,0 表示 int,0.0 表示浮点/双精度值,而 null 表示字符串/对象值。
如果将数组声明为实例变量,则在调用该对象时会使用默认值对其进行初始化。让我们看一些例子。
在 Java 的构造函数中初始化数组
如果使用默认值初始化数组,则在构造函数中初始化数组没有意义,因为 Java 隐式执行此操作。
在这个例子中,我们在类中声明了一个数组,然后在构造函数中初始化它,因此,当调用构造函数时,数组被初始化。请参考下面的示例。
public class SimpleTesting{
int a[];
public SimpleTesting() {
a = new int[]{0,0,0};
}
public static void main(String[] args){
SimpleTesting st = new SimpleTesting();
System.out.println("Array Elements");
// Accessing elements
for (int i : st.a) {
System.out.println(i);
}
}
}
输出:
Array Elements
0
0
0
我们可以在不使用构造函数的情况下完成上述任务,并看到我们为两个代码示例获得了相同的输出。我们这里没有提到初始化值,但是 Java 隐式地为我们做了这件事。请参考下面的示例。
public class SimpleTesting{
int a[] = new int[3];
public static void main(String[] args){
SimpleTesting st = new SimpleTesting();
System.out.println("Array Elements");
// Accessing elements
for (int i : st.a) {
System.out.println(i);
}
}
}
输出:
Array Elements
0
0
0
用新值在构造函数中初始化数组
如果你想设置除默认值之外的新值,使用构造函数初始化是一个好主意。在这个例子中,我们传递了其他值,并在调用构造函数时初始化数组。请参考下面的示例。
public class SimpleTesting{
int a[];
public SimpleTesting() {
a = new int[]{5,5,5};
}
public static void main(String[] args){
SimpleTesting st = new SimpleTesting();
System.out.println("Array Elements");
// Accessing elements
for (int i : st.a) {
System.out.println(i);
}
}
}
输出:
Array Elements
5
5
5
在 Java 的构造函数中初始化数组
我们也可以在构造函数中创建一个数组,以避免声明和初始化两步过程。它将在单个语句中完成任务。请看,在这个例子中,我们在构造函数中创建了一个数组并同时访问它以显示数组元素。请参考下面的示例。
public class SimpleTesting{
public SimpleTesting() {
int a[] = {0,0,0};
System.out.println("Array Elements");
// Accessing elements
for (int i : a) {
System.out.println(i);
}
}
public static void main(String[] args){
SimpleTesting st = new SimpleTesting();
}
}
输出:
Array Elements
0
0
0
相关文章
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
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。