迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 的构造函数中初始化数组

作者:迹忆客 最近更新:2023/09/17 浏览次数:

本文介绍了如何在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便