<< Back Computer Science Club

November 05 2019 - Java 4 (arrays 1, loops, increment and number modifications)

At this meeting, we went over several types of loops and started with some arrays.




Intro to Arrays

Arrays are lists of the same data type stored in one variable. For example, you could have an array of 10 integers, meaning there are ten different numbers stored in one variable as a list.


Defining an array
An array can be created by using [] next to a data type when creating a variable. You can then place multiple values separated by commas within {} as the value of the array. Like this:

String[] listOfStrings = {"Hello", "String 2", "How are you doing?", "Computer Science Club!"};

or

int[] listOfInts = {1, 5, 2, 3, 5, 7, 2};

and the same with other data types.


These kinds of arrays are fixed in size. You can't add or remove values, only set ones that are already made.

You can create an empty array like this:

double[] listOfDoubles = new double[5];

where the number in the brackets is the size of the array. We'll learn what "new" is (and how to create instances) on a later date.


Indexes in Java
To access values from any sort of array or like anything else, the first index of that list is 0, second is 1, and so forth.

For example: in "Hello friend!" the character 'H' is the 0th index, and the character '!' is the 12th index. Even though that String has 13 characters, there is no 13th index.


Getting values from an array
To get a value from an array, simply refer to the variable name and use [i] after it where i is the index.

For example:

String[] listOfStrings = {"Hello", "String 2", "How are you doing?", "Computer Science Club!"};

System.out.println(listOfStrings[0]);
//Hello

System.out.println(listOfStrings[2]);
//How are you doing?



Setting values within an array
To set a value within an array, refer to the variable name with the [i] like above and use the = to set a value, like with any other variable.

For example:

listOfStrings[0] = "What is up?";
//Hello is replaced with What is up? in the array



Getting the length of an array
To get the length of an array, call .length from the array. Like this:

int lengthOfList = listOfStrings.length;
//lengthOfList equals 4 in this case


Because the length starts at 1 (an array with only one element would return 1, not 0) and because the index of the array starts at 0, you would not be able to retreive the last element of the array using only array.length (you'd get an error).

You'd have to do listOfStrings.length - 1 since the the last index is one less than the length (index starts at 0 you get it now).




Loops

Loops allow for the same code to be run multiple times. You can use them to keep doing something over and over again until something happens, or to loop through an array and do something with each value.
You'll see what I mean.

for loop
A for loop has 3 parts, an initialization, a termination, and an increment. Here's a template:

for ([init]; [term]; [inc]){


}


In the initialization, you create or set a variable to a starting value. Typically, this is an integer.
In the termination, you put a boolean statement (something you'd have in an if statement). While this is true, the loop will run. As soon as it's false, the loop will end.
In the increment, you put a math statement, modifying the variable that you created to eventually make the termination statement false.
Here's an example:

for (int i = 0; i < 5; i++){

 //code

}


In this example, the code inside will run 5 times (i starts at 0 and then 1 is added every time the loop runs (see the section on increment and number modifications below), but will stop when i = 5, because 5 < 5 is false). Get it?



while loop
The while loop is a bit less complicated, but not as useful as the for loop. This only has one parameter, a termination. The other information (like the stuff stored in a for loop) would have to be created and stored elsewhere.

boolean isCool = true;

while (isCool) {

 //does stuff over and over again until isCool is set to false

}




do while loop
The do while loop is just like a while loop, but it will always be run at least ONE time.
Basically, it runs once (regardless of condition), then checks to see if the condition is true to keep running, where a while loop will only start if the condition is true.

do {

 //code

} while (isCool);




Increment and Number Modifications

Adding, multiplying, subtracting, and dividing a number
To add a value to a number and save it, you can do the following:

int number = 0;

number = number + 5;


5 is added to the variable, and then it is saved by setting the new value to that same variable. So number is now 5 (since it was 0 + 5).

You can replace the + with a -, *, or / dpeending on if you want to subtract, multiply, or divide.



A shorter way to add, multiply, subtract, and divide (and save) a number
What if there was a shorter way of doing

number = number + 5;

Oh wait! There is!

number += 5;

Writing it like that does the same thing as adding that number and saving it to the variable. You can do the same with -= (subtract), *= (multiply), and /= (divide).



An even shorter way to add or subtract 1 from a number
What if there was a shorter way of doing

number = number + 1;

Oh wait. We literally just had a shorter way.

number += 1;

How could it get any shorter? Lol well it can.

number++;

That adds 1 to that variable's current number. And guess what?

number--;

That subtracts 1 from that variable's current nmumber.

Don't try number++5 that's not a thing. This only works with adding and subtracting ONE.

That's useful for the for loop we saw above (instead of typing i = i + 1 in the increment section, you can just use i++. However, both ways work.




If you have any questions, ask them at Computer Science Club!