TAKE NOTE THAT THIS IS NOT A .JAVA FILE BECAUSE IT WILL BE A PLAIN TEXT FILE. INSTEAD, JAVA WILL BE COMPILED VIA THE METHOD SHOWN IN THIS VIDEO.
TL;DW
Skip to steps 3 - 5 if "NppExec" is already installed.
Skip to step 5 to execute the Java file that is already in the desired directory.
Arrays
An array is a collection of variables of the same type.
When you need to store a list of values, such as numbers, you can store them in an
array, instead of declaring separate variables for each number.
To declare an array, you need to define the type of the elements
with square brackets.
For example, to declare an array of integers:
The name of the array is arr. The type of elements it will hold is int.
Now, you need to define the array's capacity, or the number of elements it will hold. To accomplish this, use the keyword new.
int[] arr = new int[5]; The code above declares an array of 5 integers.
In an array, the elements are ordered and each has a specific
and constant position, which is called an index.
To reference elements in an array, type the name of the
array followed by the index position within a
pair of square brackets.
Example:
This assigns a value of 42 to the element with 2 as its index.
Note that elements in the array are identified with zero-based index numbers, meaning that the first element's index is 0 rather than one. So, the maximum index of the array int[5] is 4. Initializing Arrays
Java provides a shortcut for instantiating arrays of primitive types and strings.
If you already know what values to insert into the array, you can
use an array literal.
Example of an array literal:
Place the values in a comma-separated list, enclosed in curly braces.
The code above automatically initializes an array containing
4 elements, and stores the provided values.
Array Length
You can access the length of an array
(the number of elements it stores) via its length property.
Example:
Arrays
Now that we know how to set and get array elements, we can
calculate the sum of all elements in an array by using loops.
The for loop is the most used loop when working with arrays, as we can use the length of the
array to determine how many times to run the loop.
In the code above, we declared a variable sum to store the result and assigned it 0.
Then we used a for loop to iterate through the array,
and added each element's value to the variable.
Result: (Program.java)
Enhanced for Loop
The enhanced for loop (sometimes called a "for each" loop) is used to traverse elements in arrays.
The advantages are that it eliminates the possibility of bugs and makes the code easier to read.
Example:
The enhanced for loop declares a variable of a type compatible with the elements of the
array being accessed. The variable will be available within the
for block, and its value will be the same as the current array
element.
So, on each iteration of the loop, the variable t will be equal to the corresponding element in the
array.
Result: (Program1.java)
Multidimensional Arrays
Multidimensional arrays are array that contains other
arrays. The two-dimensional array is the most basic
multidimensional array.
To create multidimensional arrays, place each array within
its own set of square brackets.
Example of a two-dimensional array:
This declares an array with two arrays as its elements.
To access an element in the two-dimensional array, provide two
indexes, one for the array, and another for the element
inside that array.
The following example accesses the first element in the second array
of sample.
You can get and set a multidimensional array's elements
using the same pair of square brackets.
Example:
int[][] myArr = { {1, 2, 3}, {4}, {5, 6, 7} };
myArr[0][2] = 42;
int x = myArr[1][0]; // 4
The above two-dimensional array contains three arrays. The first
array has three elements, the second has a single element
and the last of these has three elements.
In Java, you're not limited to just two-dimensional arrays. Arrays can be nested within arrays to as many levels as your program needs. All you need to declare
array with more than two dimensions, is to add as many sets of empty
brackets as you need.
However, these are harder to maintain.
Remember, that all array members must be of the same type.
Result: (Program2.java)
Arrays
Think of arrays as boxes that you put you data but instead of starting to count from 1 with arrays the index starts from 0.
So you have five boxes from 0-4. Like this.
Array index 0 1 2 3 4
Int value 9 8 7 6 8
Enhanced for Loop
The Colon : in the For loop is read as "in"..
When the colon : is embraced with parenthesis () it is read as "eyes" (:)
Multidimensional Arrays
The multidimensional array ARE array that CONTAIN OTHER ARRAY.(need to capitalize here)
Meaning:
int[ ][ ] sample = { {1, 2, 3}, {4, 5, 6} };
int x = sample[1][0];
System.out.println(x);
// Outputs 4
The output is 4 because the first "[]" chooses the array of the two categories (for example pretend {1,2,3} and {4,5,6} are 2 arrays with something inside.
So the first "[]" chooses the BRACKET, not element inside the array, and the second "[]" chooses the ELEMENT inside the array.
FOR SIMPLE and EASY understanding example:
I have 2 boxes with number plates in them. Box one has 5,6,8 and box two has 7,8,9.