Create an array using two methods:
- Create an empty array and determine the length
2. ex.
String[] array = new String[n]
with n being the length of the array - Create an array that has elements instead of a set length
3. ex.
String[] array2 = new String[]{e1,e2,e3, ..., n}
Add elements into the first array separately
- For practice purposes, there is no need to fill entire array if there is no need to.
- The array starts at 0, so the length will be
array.length -1
at all times. - If there is an empty array
- The array starts at 0, so the length will be
- There should be no need to add elements to the other array as that was already created.
import java.util.Arrays
will let us see what is inside the array when we print to the console
If we do not use this, it will print out the name of the array object rather than the content within
the array. It looks something like this: [Ljava.lang.String;@4c873330
.
Arrays.toString(arrayName)
will convert the array into an array of strings rather than just displaying
the name of the object.
Now that the arrays are created and show that they can be printed as arrays, we can display each individual element separately.
For the purpose of practice, we will be using both the for-each and the for loops.
For-Each
I have learned that the for-each loop is easy to use for iterating through simple arrays like these two examples.
When writing the loop, we can say to ourselves "For each element
in arrayName
, iterate through arrayName
and display".
The code is written for(String element : array){ code }
. For an easier time to know what we are referencing, it
is easy to have element be a singular form of the name of the array or whatever element you are wanting to
iterate through in the array. For this code, we are going to iterate and display the elements on separate lines.
Therefore, we will use System.out.println()
within the loop or it will be out of scope.
For Loop
The for loop contains more code than the for-each loop, but operates the same way.
It is written as for(int i; i <= array.length -1; i++){ code }
.
It will result in the same thing.
-
The first statement will execute one time before the execution of the code block.
-
The second statement is the condition that determines if the loop ends or continues.
-
The third statement is executed every time after the code block. It will not execute once the condition is no longer true.
As with the for-each loop, the print statement needs to be inside of the loop or it will not be in scope.
Important note:
- if you are using
<
or>
to comparei
andarray.length
, do not add-1
afterarray.length
or will
result in it not completely reading the array. - if you are using
<=
or>=
to comparei
andarray.length
, ensure you are usingarray.length -1
or will
result in anArrayIndexOutOfBoundsException
from trying to read a non-existent index.
I added an extra section for testing out the for loop with integers for displaying. You can use a for loop to
solve expressions such as the sum, difference, product, and difference between them. The code looping through
the integer is just showing the entire list of integer.
- They operate slightly different from Strings
- if there is nothing in the index, it will place a 0 instead of null
- You can use the for loop to find the sum within the array
For extra practice, we can find the sum of the integers.