I have an issue with links in java.
I have main() function and function a (ArrayList<String>).
In a() I create an ArrayList and relink the given array list to the new one. I check in a() that old one isn't empty.
After that I'm returning to main() and my array list is empty.
import java.util.ArrayList;
public class Main {
public static void a(ArrayList<String> arrayList_){
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("b");
arrayList_=arrayList;
System.out.println(arrayList_.get(0));
}
public static void main(String[] args){
System.out.println("a");
ArrayList<String>arrayList=new ArrayList<>();
a(arrayList);
System.out.println(arrayList.get(0));
}
}
After trying to run this code:
a
b
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at Main.main(Main.java:21)
Expected:
a
b
b
Why is that so?