如何在Java中将ArrayList对象作为函数参数传递?
Java中的 ArrayList 类基本上是一个可调整大小的数组,即它可以根据我们向其中添加或从中删除的值动态地增长和缩小大小。它存在于Java.util 包中。如果我们想将 ArrayList 作为参数传递给函数,那么我们可以使用下面提到的语法轻松完成。
例子:
ArrayList list = new ArrayList<>();
modifyList(list);
public static void modifyList(ArrayList list){
list.add(69);
list.add(98);
}
在上面的代码中,我们创建了一个名为“list”的 ArrayList 对象,然后将其传递给名为 modifyList 的函数。我们对此列表所做的更改(即我们添加到列表中的值)将显示在主函数中的原始列表中。发生这种情况是因为对象引用在Java中是按值传递的 Java是严格按值传递的。
问题陈述的实现:
Java
// Java program to pass an
// ArrayList as an argument
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating an ArrayList Object of Integer type
ArrayList list = new ArrayList<>();
// Inserting some Integer values in ArrayList
list.add(3);
list.add(57);
list.add(7);
// list after insertions : [3, 57, 7]
// Displaying the ArrayList
System.out.print("ArrayList after insertions: ");
display(list);
// Passing the ArrayList as an argument for
// modification The modifications done to the list
// inside the function will appear inside the
// original ArrayList inside main()
modifyList(list);
// list after modifications : [3, 57, 7, 20, 98]
// displaying the ArrayList after modifications
System.out.print("ArrayList after modifications: ");
display(list);
}
// Function to modify the arrayList
public static void
modifyList(ArrayList parameterList)
{
parameterList.add(20);
parameterList.add(98);
}
// Function to display the array List
public static void display(ArrayList list)
{
System.out.println(list);
}
}
Java
// Java program to pass an
// ArrayList as an argument
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// Creating an ArrayList Object of Integer type
ArrayList list = new ArrayList<>();
// Inserting some Integer values in ArrayList
list.add(3);
list.add(57);
list.add(7);
// list after insertions : [3, 57, 7]
// Displaying the ArrayList
System.out.print("ArrayList after insertions: ");
display(list);
// The changes made to the ArrayList inside this
// function will not affect the original ArrayList
// that we pass to it as we are simply creating the
// new instance of ArrayList and making our
// parameterList point to it
callByValue(list);
// list after function call : [3, 57, 7]
// displaying the ArrayList after calling
// callByValue
System.out.print(
"ArrayList after the function call: ");
display(list);
}
public static void
callByValue(ArrayList parameterList)
{
// Creating a new instance of ArrayList
parameterList = new ArrayList<>();
// Inserting Integer values to the new ArrayList
// created inside the function
parameterList.add(88);
parameterList.add(200);
parameterList.add(89);
// Displaying our new ArrayList
System.out.print(
"New ArrayList inside the function: ");
display(parameterList);
}
// Function to display the array List
public static void display(ArrayList list)
{
System.out.println(list);
}
}
ArrayList after insertions: [3, 57, 7]
ArrayList after modifications: [3, 57, 7, 20, 98]
Java是如何通过值传递的?
让我们假设我们在 main函数中创建的 Array 列表对象指向地址 1000。当我们将此对象传递给 modifyList函数时,地址 1000 被传递给它,并且对象“parameterList”也开始指向相同的内存中的位置作为“列表”的位置,这就是我们说对象引用在Java中按值传递的原因。之后,当我们对“parameterList”进行修改时,也会对原始“list”进行相同的更改。这个概念与在 C++ 中将指针变量作为参数传递时发生的情况非常相似。
ArrayList list = new ArrayList<>();
callByValue(list);
public static void callByValue(ArrayList parameterList){
parameterList = new ArrayList<>();
parameterList.add(88);
parameterList.add(200);
parameterList.add(89);
}
在上面的代码中,当我们将列表传递给 callByValue函数时,“parameterList”开始指向内存地址,即 1000。但是当我们创建 ArrayList 的新实例并让“parameterList”指向它时,“parameterList”开始指向到新的内存地址(比方说)2000。在此函数中对 ArrayList 所做的更改将不再影响内存地址 1000 处的 ArrayList。因此,主函数中的“列表”不受影响。
下面是问题陈述的实现:
Java
// Java program to pass an
// ArrayList as an argument
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// Creating an ArrayList Object of Integer type
ArrayList list = new ArrayList<>();
// Inserting some Integer values in ArrayList
list.add(3);
list.add(57);
list.add(7);
// list after insertions : [3, 57, 7]
// Displaying the ArrayList
System.out.print("ArrayList after insertions: ");
display(list);
// The changes made to the ArrayList inside this
// function will not affect the original ArrayList
// that we pass to it as we are simply creating the
// new instance of ArrayList and making our
// parameterList point to it
callByValue(list);
// list after function call : [3, 57, 7]
// displaying the ArrayList after calling
// callByValue
System.out.print(
"ArrayList after the function call: ");
display(list);
}
public static void
callByValue(ArrayList parameterList)
{
// Creating a new instance of ArrayList
parameterList = new ArrayList<>();
// Inserting Integer values to the new ArrayList
// created inside the function
parameterList.add(88);
parameterList.add(200);
parameterList.add(89);
// Displaying our new ArrayList
System.out.print(
"New ArrayList inside the function: ");
display(parameterList);
}
// Function to display the array List
public static void display(ArrayList list)
{
System.out.println(list);
}
}
ArrayList after insertions: [3, 57, 7]
New ArrayList inside the function: [88, 200, 89]
ArrayList after the function call: [3, 57, 7]