Spaces:
Running
Running
TurkuBasicOOPinJava
/
Week 3: Objects, files and exceptions
/3a. Objects as parameters and return values
| 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: | |
| [] | |
| [] | |