Java:为什么函数的 ArrayList 参数会影响另一个不同的 ArrayList

huangapple 未分类评论57阅读模式
英文:

Java: Why is the ArrayList Parameters of a function effecting a different ArrayList

问题

我在编程方面相对较新,所以如果这个问题很愚蠢,我感到抱歉。为什么ArrayList numbers 会受到函数 changeParam 中使用Arraylist作为参数的影响,例如...

public class Test {
    static ArrayList<Integer> numbers = new ArrayList<>();
    
    public static void main(String[] args) {
        for(int i = 0; i <= 100; i++){
            numbers.add(i);
        }
        changeParam(numbers);
        System.out.println(numbers);
    }
    
    public static void changeParam(ArrayList<Integer> A){
        A.clear();
    }
}

为什么在打印numbers时它会变成空的呢?

英文:

I'm relatively new to coding so I apologize if this question is stupid. Why is the Arraylist numbers getting affected by what is happening within the function changeParam with the parameter of Arraylist for example...

public class Test {
static ArrayList&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();
 public static void main(String[] args) {
     for(int i = 0; i &lt;= 100; i++){
         numbers.add(i);
        }
     changeParam(numbers);
     System.out.println(numbers);
    }
 public static void changeParam(ArrayList&lt;Integer&gt; A){
A.clear();
}
}

Why when numbers is printed does it come out empty?

huangapple
  • 本文由 发表于 2020年3月15日 06:16:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/60687802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定