Spaces:
Running
Running
File size: 679 Bytes
e724e0d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
Objects as parameters -> passed as a REFERENCE
so
a method can change/MODIFY INPLACE the input list it receives as a parameter
import java.util.ArrayList;
public class Example {
public static void main(String[] parameters){
ArrayList<Integer> numbers = new ArrayList<>();
for (int i=1; i<5; i++) {
numbers.add(i);
}
System.out.println(numbers);
increase(numbers);
System.out.println(numbers);
}
public static void increase(ArrayList<Integer> list) {
for (int i=0; i<list.size(); i++) {
list.set(i, list.get(i) + 1);
}
}
}
Program outputs:
[1, 2, 3, 4]
[2, 3, 4, 5]
|