In Java there are two ways to create an 'array'. Arrays are simple fixed sized arrays.
// Array Example
int[] name = new int[size];
ArrayList are dynamic sized arrays that implement list interface.
// ArrayList Example
ArrayList<Type> name = new ArrayList<Type>();
An Array is a fixed size data structure while ArrayList is not. You dont need to mention the size of an ArrayList when creating its object. Even if you do specify an initial capasity you can always add more to the ArrayList. for example:
// Array
int[] arr = new int[3]; // the array size is fixed at 3 objects.
arr[0] = 1;
arr[1] = 2;
arr[2] = 3; // we cant add anymore objects to the array without getting a NullPointerExeption
// ArrayList
ArrayList<Integer> arrL = new ArrayList<Integer>();
arrL.add(1);
arrL.add(2);
arrL.add(3);
arrL.add(4);
// we can add as many elements as we want essentially
Arrays can contain both primitive data types aswell as objects of a class. however ArrayList only allows objects, not primitive data types.
While loops take a boolean expression and while that expression is true it will run the code inside of the body of the while statement.
// for example a While loop with a boolean condition
int i = 0;
while (i < 10) {
System.out.println(i); // this code would run 10 times before exiting
i++; // however the i variable would be checked 11 times
}
System.out.println("done!");
A While loop is essentially a more complex if statement. the if statement will check if the statement is true and if it is then it will run the body of the if statement. the while loop expands on this by continuing to run the body of the loop until a false condition is met. This can be a problem however if our condition never equates to false, we are then stuck in a infinite loop and the program will never end on its own. in our xample above the while loop check to see if i is less then ten and if it is it will print the value of i. once the value of i has been incremented to 10 the loop breaks and then our program prints "done".
// this is an example of a infinite loop
int i = 0;
while (i < 1) {
System.out.println(i);
i--;
} // This code will continue to print i forever if we let it, or at least until it crashes
Sometimes however we can purposfully write code that loops forever. This code still needs to come to an end to become useful though. this is where the break statement comes in handy. a break statement will end the loop when ever the program reads it. so for example, for what ever reason you would want to do this(normaly we wouldnt want to), say we have a while loop thats initial boolean condition is true. once we realize by mistake or not we say once somethingRidiculous happens we want the loop to end. so we would put an if statement nested inside of our while loop to then call the break command.
while (true) {
...
// some code we want to run
...
if (somethingRidiculous() == true) {
break;
}
}
we should try our best to not write loops like this because they can be hard to read or understand, so consider them a last resort case if you boolean condition cant be at the top.
A For loop is a variant of a While loop, its tailored to step though a loop for a certain number of times. for example we want to print numbers 1 through 10.
for (int i = 0; i < 10; i++) {
System.out.println(i + 1);
}
When creating a For loop, you must include 4 parts, the init, test, increment, and the body. the init, test, and increment are separated by semi colons.
the Init code runs once to set everything up at the beginning of the loop. the most common init is "int i = 0;" this value only exist in the loop. the widespread use of i for the init variable is almost ubiquitous when writing For loops. its use is so common that other programmers may be confused when reading your code with out it. this also extends to using names like "j" and "k" if i is already declared. Never use lowercase L "l", since it looks just like the digit one "1" in many fonts(like the one im writing this in).
The Test is just a boolean evaluation, generally "i < someNumber".
If the test returns true then the bosy of the For loop then runs once. the body consist of the code you want to run over multiple times.
the increment runs after the body of the code executes, and then steps to test.