Arrays stores multiple values in a single variable. The array stores multiple variables only of the SAME DATA TYPE. They are used for organizing data, and making coding more efficient. The problem with arrays is that it only has a fixed value of elements, so once created, the amount of variables in a array cannot change.
This block of code prints element 0, which is "Oppenheimer".
This block of code prints changes element 0 which is "Oppenheimer" to "Barbie".
Accessing the elements in a array is super easy.
First, remember each element is numbered starting at 0. So in an array that has Strings of movies {Oppenheimer, The Fablemans, 1917}, Oppenheimer is Element 0.
Like in the example on the side, ArrayName[a], the variable a is where you put the element number you want to access. You can then use it like it was it own variable.
You can also change an element of an array by first accessing the array using ArrayName[a], then adding the variable you'd want it to change too. If Oppenheimer was element 0 in a String array named movies and I want to change it to the movie Barbie. The call would be: movies[0] = "Barbie";
The best way to traverse through an array easily is using a loop. The for loop, and for each loop are most commonly used. In the LOOPS tab , the for each loop for traversing arrays are already covered. Right now I'll be showing how to traverse through an array in a for loop.
First, add the statement for(). After, within the brackets, you declare what element you want to start from in the array. So if I wanted to start traversing the elmenst starting at element 1 I would do int i = 1.
The next statement is where you want the traversing to end. This is where the code is told to stop looking through all the elements. If we wanted to got through the whole array (one by one) we would do i < arrayName.length;
The last statement is what increments you would traverse the array. Do you want to look through every other element? Every 3rd array? It all depends on you. IF we wanted to go through each element without skipping, we would do i++.
Finally, add the code you want to do for each element. the easiest example to show is the print method, where it prints every element on a new line.
A code we can use for that is System.out.println (i);
Array Lists stores multiple values in a single variable, BUT one thing different from arrays is that it isn't at a fixed value, it can be resized. they can be modified in mutliple different rather than only changing the variable names.
An Array List can only take in objects not primitive data types. to get around this, we can use Wrapper Classes which act the same as primitive data types, but they immutable and it disguises the primitive type as an Object itself. The way to call it is a little different though, the chart beside it shows you all the Wrapper classes you should need to know.
BOTTOM CODE: This is modified with step 5.
Declaring an array list is super similar to declaring an array, but it's a bit more complex. Here are the steps:
Follow the syntax ArrayList<> "place name here" = new ArrayList <>()
INSIDE the brackets <>, add your data type. REMEMBER ArrayList doesn't take primitive data types, only objects. We can go around this by using wrapper types. For example if I wanted an ArrayList to contain doubles, I would use Double.
Beside it where it says "place name here" add your ArrayList name. Example I'll name my ArrayList "prices".
After the equals sign go to the next <> brackets and add your data type/object. We'll use Double again as we did that last time.
Optional: The () brackets is where you can input a number on how large your array is (how many elements there will be). This is option as an ArrayList is immutable so it doesn't matter size it is.
HOW TO CALL: ArrayName.get(int index)
This method is pretty straightforward. It will return the element name at that index. This is helpful if you want to easily access the array's names. In the brackets you add the index you want access at, and it'll access the element name for you.
KEY EXAMPLE:
IF ArrayList is named "Games" and the ArrayList looks like this: {"Halo", "Call Of Duty", "Starfield"} and I want to access the element "Starfield" I should code:
Games.get(2);
HOW TO CALL: ArrayName.add(int index, "your variable name") and ArrayName.add("your variable name")
This method is pretty straightforward. It will add an element of your choice, inserting it before the item at the specified index. IF you don't want to specify you can just add your element with no index and it'll automatically add it at the END of the ArrayList.
KEY EXAMPLE (without index):
IF ArrayList is named "Books" and the ArrayList looks like this: {"HarryPotter", "LOTR", "The Outsiders"} and I want to add the element "Dog Man" at the end of the ArrayList I should code:
Books.add("Dog Man");
IF ArrayList is named "Books" and the ArrayList looks like this: {"HarryPotter", "LOTR", "The Outsiders"} and I want to add the element "Dog Man" at the start of the ArrayList I should code:
Books.add(0, "Dog Man");
HOW TO CALL: ArrayName.set(int index, "your variable name")
This method will change a element of your choice to a specified name using the index you pass. In the first parameter, add the index of where you want to replace the item, then after add the actual item YOU are going to replace it with.
KEY EXAMPLE:
IF ArrayList is named "Numbers" and the ArrayList looks like this: {5, 7, 3} and I want to replace the element 7 to 3, so I should code:
Numbers.set(1, 3);
HOW TO CALL: ArrayName.remove(int index)
This method will remove an element out of the ArrayList entirely, depending on the index you ask it too.
KEY EXAMPLE:
IF ArrayList is named "Numbers" and the ArrayList looks like this: {5, 7, 3} and I want to remove the element 7, I should code:
Numbers.remove(1);