Arrays and for loop

11 0 0
                                        

class AnotherArray{

             private String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};

            private String[] suit = {" club", " diamond", " spade", " heart"};

            private String title;

            private String[][] card;


             AnotherArray(String title){

                       this.title =title;

                       card = new String[rank.length][suit.length];

                       for(int i=0;i<rank.length;i++){

                                for(int j=0;j<suit.length;j++){

                                         card[i][j] = rank[i].concat(suit[j]);

                                }

                      }

            }

           public void displayAllCards(){

                     for(int i=0;i<rank.length;i++){

                            for(int j=0;j<suit.length;j++){

                                      System.out.println(card[i][j]);

                             }

                   }

           }

}

public class ArrayDemo{

            public static void main(String[] args){

                     AnotherArray aa = new AnotherArray("Card Display");

                     aa.displayAllCards();

            }

}


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I mentioned last time that there are other control flow statements beside the if else statement. And these are the switch statement, while loop, do while loop, for loop and the enhance for loop.

A loop is like a race track. You keep on running on the track over and over again until you reach the number of times required, before you stop or getting out of the track. This is the same with the for  loop. You keep on looping over and over again until the condition is not meet. Then you get out of the loop. A for loop has a format like this:

for(initialization; condition; increment){

       your code;

}

Compare it below:

for(int i = 0; i < rank.length; i++){}

"int i = 0;" we declare a variable named "i" with a type integer to be initialize to zero;

 "i < rank.length;" This is the condition. If the "i" is less than the length of the array named rank;

"i++;" we increment the variable "i". This is equivalent to i = i + 1;

This for loop is being read like this. If this is true that the variable "i" starting with zero is less than with the length of the rank which is 13, we loop through the for loop body and then increment the i to one; then, go back to the condition again. This time using i =1. Until we reach to false, that will be our signal to get out of the loop. So what is the body of our loop? It is another loop. An inner loop to be precise. This inner for loop has a length of 4 which is the length of the array suit. It has the same function with the outer loop. Inside the body of the inner loop is this, "card[i][j] = rank[i].concat(suit[j]);". This variable named card is an array.

So, what is an array? An array is an object. We used an array to receive multiple inputs of objects to be process in our program. An array can be declare like this:

"private String[] suit = {" club", " diamond", " spade", " heart"};"

Again the private is just the modifier on how we accessed the field array.

"String[] suit;" - This is just the same in declaring a variable. The only difference is the bracket.

"suit = {" club", " diamond", " spade", " heart"};" -  this is one way of initializing an array. You put the braces and encode the values inside it separated by a comma and then end it with a semi colon. 

String[] suit = String[4];

suit[0] = club;

suit[1] = diamond;

suit[2] = spade;

suit[3] = heart;

This is another way of declaring and initializing an array; The number 0 to 3 are called the indexes that we used to count every array. It starts with zero and ends with length - 1 of the size of the array. If the length of the suit is 4, then the index is from 0 to 3. If the length of the rank is 13, then the index is from 0 to 12. Those values like club, diamond, heart and spade are called elements. The suit and rank is the name we chose to call our array.

"String[] suit = String[4];" - So, this should be read like this. We created an array having a named suit with a data type of String and length of 4. We can accessed the array using its indexes. That is why we used that for loop. The inner loop has a maximum value of the length of the array suit which is 4. So, it loops from 0 to 3. The outer loop has a maximum value of the length of the array rank which 13. So it loops from 0 to 12.

 What about this "card[i][j] "? This is a two dimensional array. "j" is the variable of the inner loop with a value from 0 to 3 and the "i" is the variable of the outer loop with a value of 0 to 12. So, loop within a loop function like this.

outer loop: let i = 0;

card[0][j]

inner loop: let j = 0;

card[0][0];

inner loop: loops again: let j = 1;

card[0][1];

until let j = 4; conditon 4 < 4? False since 4 is equal to 4

get out of the inner loop:

outer loop say: then, let i = 1;

card[1][j];

inner loop: will loop four times again until false.

After the condition in the outer loop becomes false, that is the only time that we get out of that loop within a loop.

We equate card[i][j] to "rank[i].concat(suit[j]);" 

rank[i] - "i" is just the index. it means we want to get that specific element inside the array rank.

suit[j] - "j" is just the index. Same here.

.concat - means concatenate. Concatenate functions like addition but instead of numbers we used Strings or letters. Those numbers 2 and so on is inside the "" quotation mark which signifies that the number 2 is not a decimal number but a letter or a String. so card[i][j] is equal to the addition of two strings coming from array rank and array suit. 

public void displayAllCards(){} -  this is a method named displayAllCards with no return type. Inside the method's body we command to print out each card using the System.out.println().


Java codes i learned onlineWhere stories live. Discover now