迹忆客 专注技术分享

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

Java 中将数组传递给方法

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

本文介绍如何将数组传递给 Java 中的方法。我们还列出了一些示例代码以帮助你理解该主题。

Java 是一种强大的面向对象的编程语言;它是一种通用编程语言,可用于各种目的。让我们学习 Java 中的数组和方法,并了解如何将数组传递给方法。


Java 中的数组

数组是相同数据类型的固定大小的集合。它们作为连续块存储在内存中,它使我们能够在恒定时间随机访问数组的任何元素。

这种随机访问是可能的,因为数组中的每个元素都有一个与之关联的专用索引。我们不必遍历整个数组来到达特定元素。数组索引从 0 开始到 n-1,其中 n 是数组的长度。

以下代码行解释了如何创建数组并访问其元素。

public class Main{
    public static void main(String[] args)
    {
      int[] arr; //Declaration
arr = new int[5]; //Creation
//Initialization
arr[0] = 1;
arr[1] = 3;
arr[2] = 5;
arr[3] = 7;
arr[4] = 9;
//Accessing Array Elements
System.out.println("Second Element: " + arr[1]);
System.out.println("Fourth Element: " + arr[3]);
}
}

输出:

Second Element: 3
Fourth Element: 7

Java 中的方法

方法被定义为一组可用于完成特定任务的指令。它们用于增加我们代码的可重用性。

例如,如果我们想找到 110 之间所有数字的阶乘,最好为 factorial 定义一个方法并首先调用该方法 10 次,而不是重写阶乘 10 的整个逻辑不同的时间。

Java 中的方法与其他编程语言中的函数非常相似。唯一的区别是方法与对象相关联,而函数则没有。由于 Java 是一种完全面向对象的语言,因此我们只有 Java 中的方法。


在 Java 中将数组传递给方法

一个方法可能会也可能不会接受一组固定的参数。参数可以是我们需要用来定义方法体的任何变量。

在阶乘方法的示例中,参数可以是我们需要找到其阶乘的数字。但是如果我们需要将整个数组传递给一个方法呢?

在方法声明中,我们需要告诉 Java,该方法必须接受某个数据类型的数组才能将数组传递给方法。使用数组的数据类型和方括号表示参数为数组。

//Method Declaration
public static void addTen(int[] arr)
{
    //Method Body
}

每当调用该方法时,我们都需要将数组的名称传递给该方法。下面的示例显示了一个完整的代码,其中包含一个接受数组并调用该方法的方法。

public class Main
{
    public static void addTen(int[] arr)// int[] denotes that the parameter is an array
    {
        for(int i = 0; i < arr.length; i++)
        {
            arr[i] += 10;
        }
    }
    public static void main(String[] args)
    {
        int[] arr = {1, 3, 5, 7, 9};
        addTen(arr);//Simply pass the name of the array to the method
        for(int i = 0; i < arr.length; i++)
        {
            System.out.print(arr[i] + " ");
        }
    }
}

输出:

11 13 15 17 19 

将数组传递给 Java 中的方法

考虑另一个例子,我们将两个相同长度的数组传递给一个方法。该方法应该打印两个数组的总和。

public class Main{
    public static void addArrays(int[] arr1, int[] arr2)//Two arrays are mentioned in the method declaration
    {
        for(int i = 0; i < arr1.length; i++)
        {
            int sum = arr1[i] + arr2[i];
            System.out.print(sum + " ");
        }
    }
    public static void main(String[] args)
    {
        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {2, 4, 6, 8, 10};
        addArrays(arr1, arr2);//Passing two arrays and calling the method
    }
}

输出:

3 6 9 12 15

将多维数组传递给 Java 中的方法

我们还可以将多维数组传递给 Java 中的方法。我们需要根据数组的维数来指定数组元素和方括号的数据类型。

考虑以下示例,其中我们找到二维数组中存在的所有元素的总和。

public class Main
{
    public static void sum(int[][] arr)
    {
        int sum = 0;
        for(int i = 0; i < arr.length; i++)
        {
            for(int j = 0; j < arr[0].length; j++)
            {
                sum = sum + arr[i][j];
            }
        }
        System.out.print("Sum of all elements is: " + sum);
    }
    public static void main(String[] args)
    {
        int[][] arr = {
                {1, 2, 3, 4, 5},
                {2, 4, 6, 8, 10},
                {1, 3, 5, 7, 9}
        };
        sum(arr);
    }
}

输出:

Sum of all elements is: 70

了解数组如何传递给 Java 中的方法

让我们看一下并尝试了解当我们将参数传递给方法时在幕后发生了什么。

在 Java 中,参数是一种按值传递类型。这意味着每当我们将一个变量传递给一个方法时,该变量的值的副本是由该方法使用的内容而不是原始变量本身传递的。

例如,让我们考虑以下情况,其中一个方法接受一个整数并将 10 添加到该整数上。

public class Main
{
    public static void addTen(int num)
    {
        num = num + 10;
    }
    public static void main(String[] args)
    {
        int a = 12;
        addTen(a);
        System.out.print(a);
    }
}

输出:

12

你认为上面代码的输出会是什么?数字的值应该增加 10,对吗?

发生的情况是,即使将整数传递给该方法,该方法实际接收到的是该整数值的副本。因此,所有更改都对该副本进行了更改,而原始整数没有更改。然而,这只发生在像 int 这样的原始数据类型。

数组不是这种情况,因为数组不是原始数据类型,而是被视为引用堆内存中内存位置的容器对象。因此,它们存储的是内存位置的值而不是实际数据。

每当我们更改该内存位置中的某些内容时,该更改将对指向该内存位置的所有指针(或引用)可见。请记住,在 Java 中数组也是按值传递的,但该值实际上是一个内存位置。

考虑上面提到的代码,我们创建了一个方法来将 10 添加到数组的每个元素。


将数组传递给 Java 中的方法

我们经常需要将一组相同类型的数据传递给一个方法。数组最适合这些任务,我们可以将数组传递给方法。

在方法声明中,我们需要明确指定该方法应该接受上述数据类型的数组;这是使用数据类型和方括号(例如,int[] arrayName)完成的。

在调用方法时,我们可以输入数组的名称。在本教程中,我们还学习了方法如何处理数组以及如何更新存储数组元素的内存位置。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便