Notes 3 - Introduction to Java(String, Array, Loop Through an Array)

 Objectives:

Introduction

    Welcome back to Lecture 3! In previous lecture,  we were able to get to know more about data types. We were also able to get to know about conditions and loops. Now we are going to get to know more about String and I'll introduce you to another variable called Array then we are going to know how to loop through Array using loops.

String

    Strings are used to store some text.

    A String variable is a collection of characters(Including whitespaces) surrounded by double quotes.

    Example: Create a variable of type String and assign it a value.

String greeting = "Hello!";

String Length

    A String in Java is actually an object, which means it contain methods that can perform different operations for String. One of the example is length(). length() determines the length a String including white spaces. 

String greeting = "Hello!";
System.out.println(greeting.length());
// Outputs 6

More String Methods

    Aside from length(), there are also other String methods available, for example toUpperCase() and toLowerCase().

String greeting = "Hello!";
System.out.println(greeting.toUpperCase()); // Outputs HELLO!
System.out.println(greeting.toLowerCase()); // Outputs HELLO!

Finding a Character in a String

    The indexOf() methods return the index(or position) of the first occurrence of a specified text in a String (Including whitespace):

String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7

Note: Java counts positions from 0...

0 is the first in a String then followed by 1 is the second, 2 is the third...

For other useful String methods, please click here

Arrays

    Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

    To declare an array, define the variable type with square brackets: 

String[] shoes;

    We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside a curly braces:

String[] shoes = {"Nike", "Adidas", "Vans"};

    To create an array of integers, you could write:

int myNum = {5, 10, 8, 9, 5};

Access the Elements of an Array

    You can access an array element by referring to the index number.

    This statement accesses the value of the first element in shoes:

String[] shoes = {"Nike", "Adidas", "Vans"};
System.out.println(shoes[0]);
//Ouptputs Nike

Note: Array indexes starts with 0: [0] refers to the first element. [1] refers to the 2nd element and so on.

Change an Array Element

    To change the value of a specific element, refer to the index number:

shoes[0] = "Flipflops";

String[] shoes = {"Nike", "Adidas", "Vans"};
shoes[0] = "Flipflops";
System.out.println(shoes[0]);
//Now outputs Flipflops instead of Nike

Array Length

    Array length refers to the number of elements inside an array. In order to do it, we have to use the property length property:

String[] shoes = {"Nike", "Adidas", "Vans"};
System.out.println(shoes.length);
//Outputs 3

Loop Through an Array

    You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.

    The following example outputs all elements in the shoes array:

String[] shoes = {"Nike", "Adidas", "Vans"};
for (int i=0; i<shoes.length; i++) {
    System.out.println(shoes[i]);
}

Loop Through an Array with For-Each

    There is also a "for-each" loop, which is used exclusively to loop through elements in arrays:

for (type variableName : arrayName) {
  // code block to be executed
}

    The following example outputs all elements in the shoes array, using a "for-each" loop:

String[] shoes = {"Nike", "Adidas", "Vans"};
for (int i=0; i<shoes.length; i++) {
    System.out.println(shoes[i]);
}

    The example above can be read like this: for each String element (called i - as index) in cars, print out the value of i.

    If you compare the for loop and for-each loop,  you will see that the for-each method is easier to write, it does not require a counter (using the length property), and it is more readable.


Notes 4

Comments

Popular Posts