Creating and Using Arrays
Arrays let you store multiple values under one name, which is super useful when dealing with lists of related items. An array holds a fixed number of elements (individual values) of the same type.
You can create arrays in three different ways in Java. First, you can declare and then instantiate later (like int arr; followed by arr = new int10;). Second, you can do it all at once with int nums = new int5;. Finally, you can use an initializer list with values inside curly braces: int a = {1,3,5,7,9};.
To find out how many elements an array has, use the length property (notice there are no parentheses like with strings). When working with arrays as parameters in methods, remember that Java passes the memory address of the array (by reference), meaning changes to the array inside the method affect the original array.
💡 When making a copy of an array, don't use int copy = arr; - this just creates another pointer to the same array! Instead, create a new array and copy each element individually using a loop.
Traversing (going through) an array typically uses a for loop like:
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]+" ");
}