Iteration Using 2D Arrays vs Iteration Using Objects

Iteration is especially useful when going through long lists of items. Two instances of this could be iterating through a 2D Array which in this case is an array of an array of strings which allows me to create rows and columns for each of my ASCII art pieces, or a 1D array of Objects who's properties include a 1D array of strings that make up my ASCII art piece.

Let's see both of them in action:

Iteration Using 2D Arrays

Unlike the example in Mr. Mort's blog, I'll be using an aquarium and slightly edited version of the monkeys jumping on the bed rhyme and be putting them horizontally as opposed to vertically.

public class Aquarium{
    //Instantiate a 2D Array (made of columns and rows) like a grid
    String [][] aquarium;

    //Constructor ("Builds" (Constructs) aquarium in this example)
    public Aquarium(){
        aquarium = new String[][]{
            {
                "      /\\        ",
                "    _/./         ",
                " ,-'    `-:..-'/ ",
                ": o )      _  (  ",
                " `-....,--; `-.\\",
                "   `'            ",
                "                 ",
                "                 "
            },
            {
                "      /`·.¸         ",
                "     /¸...¸`:·      ",
                " ¸.·´  ¸   `·.¸.·´) ",
                ": © ):´;      ¸  {  ",
                " `·.¸ `·  ¸.·´\\`·¸)",
                "    `\\\\´´\\¸.·´   ",
                "                    ",
                "                    "
            },
            {
                "    |\\    o   ",
                "    |  \\    o ",
                "|\\ /    .\\ o ",
                "| |       (    ",
                "|/ \\     /    ",
                "    |  /       ",
                "    |/         ",
                "               "
            },
            {
                "            ___             ",
                "         _ / __)_   °       ",
                " _     .'_'-'\\ /-'-. o °   ",
                "\\'-._.'-'\\ / _\\-(O)_: O  ",
                " \\ (__\\/_ \\ '._)  _\\ o  ",
                " /.' (_.'----''./'          ",
                "                '           ",
                "                            "
            },
            {
                "   ____      ",
                "  /    \\    ",
                " /----./     ",
                "/ o    \\/|  ",
                ">        |   ",
                "\\ <)   /\\| ",
                " \\----'\\   ",
                "  \\____/    "
            }

        };
    }

    public void print(){
        System.out.println();
        System.out.println("The Sea Aquarium");
        
        int aquariumCount = aquarium.length;
        for (int i = aquariumCount; i > 0; i--){
            System.out.println(i + " Little fishies swimming in the sea...");

            for (int col = 0; col < aquarium[0].length; col++){
                for (int row = 0; row < i; row++){
                    System.out.print(aquarium[row][col] + "\t");
                }
                
                System.out.println();
            }
            System.out.println("A shark gobbled up one in a spree");
            System.out.println("The employee called the manager and the manager said, ");
            System.out.println("'No more sharks swimming in the sea!'");
            System.out.println("\n--------------------------------------\n");
            aquariumCount -= 1;  
        }

        System.out.println("No more fishies living in the sea :( \n");
        System.out.println("--------------------------------------");
        System.out.println("             THE END              ");
     
    }

    public static void main(String[] args)  {
        new Aquarium().print();
    }
}

Aquarium.main(null);
The Sea Aquarium
5 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	            ___             	   ____      	
    _/./         	     /¸...¸`:·      	    |  \    o 	         _ / __)_   °       	  /    \    	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	 _     .'_'-'\ /-'-. o °   	 /----./     	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	\'-._.'-'\ / _\-(O)_: O  	/ o    \/|  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	 \ (__\/_ \ '._)  _\ o  	>        |   	
   `'            	    `\\´´\¸.·´   	    |  /       	 /.' (_.'----''./'          	\ <)   /\| 	
                 	                    	    |/         	                '           	 \----'\   	
                 	                    	               	                            	  \____/    	
A shark gobbled up one in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

4 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	            ___             	
    _/./         	     /¸...¸`:·      	    |  \    o 	         _ / __)_   °       	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	 _     .'_'-'\ /-'-. o °   	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	\'-._.'-'\ / _\-(O)_: O  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	 \ (__\/_ \ '._)  _\ o  	
   `'            	    `\\´´\¸.·´   	    |  /       	 /.' (_.'----''./'          	
                 	                    	    |/         	                '           	
                 	                    	               	                            	
A shark gobbled up one in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

3 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	
    _/./         	     /¸...¸`:·      	    |  \    o 	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	
   `'            	    `\\´´\¸.·´   	    |  /       	
                 	                    	    |/         	
                 	                    	               	
A shark gobbled up one in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

2 Little fishies swimming in the sea...
      /\        	      /`·.¸         	
    _/./         	     /¸...¸`:·      	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	
: o )      _  (  	: © ):´;      ¸  {  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	
   `'            	    `\\´´\¸.·´   	
                 	                    	
                 	                    	
A shark gobbled up one in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

1 Little fishies swimming in the sea...
      /\        	
    _/./         	
 ,-'    `-:..-'/ 	
: o )      _  (  	
 `-....,--; `-.\	
   `'            	
                 	
                 	
A shark gobbled up one in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

No more fishies living in the sea :( 

--------------------------------------
             THE END              

Iteration Using Objects

Now let's see this example but in the form of objects! Because the static key word, I can create a class for just the fish alone which creates fish objects and also perform methods. In this sense, the class has universal methods which can be called to do something to the objects it (the class) created. It's pretty crazy cool.

By using OOP, the fishes are far easier to replicate and I can even give them other properties such as names, age, type etc. So, unlike iteration using purely 2D arrays, each fish I make can have unique properties. Both of them however are relatively easy to expand the list that the print function iterates through. However, OOP makes it far easier to manipulate the data of the fishes and the list that is iterated through.

import java.util.ArrayList;

public class AquariumFish{
    //Instantiates a static array list of fish objects (belongs to class)
    static ArrayList<AquariumFish> fishesList = new ArrayList<AquariumFish>();

    // Stores individual fishes (the strings that make up the fish) for each object
    String[] fish;

    // Name of each fish
    String name;

    //Constructor which takes the string array of fish and the name of each fish as arguments
    public AquariumFish(String[] fish, String name) {
        this.fish = fish; //body
        this.name = name; //name
        fishesList.add(this);
    }

    public static void print(){
        System.out.println();
        System.out.println("The Sea Aquarium");
        
        int aquariumCount = fishesList.size();
        for (int i = aquariumCount; i > 0; i--){
            System.out.println(i + " Little fishies swimming in the sea...");

            for (int col = 0; col < fishesList.get(0).fish.length; col++){
                for (int row = 0; row < i; row++){
                    System.out.print(fishesList.get(row).fish[col] + "\t");
                }
                
                System.out.println();
            }
            System.out.println("A shark gobbled up " + fishesList.get(i-1).name + " in a spree");
            System.out.println("The employee called the manager and the manager said, ");
            System.out.println("'No more sharks swimming in the sea!'");
            System.out.println("\n--------------------------------------\n");
            aquariumCount -= 1;
        }

        System.out.println("No more fishies living in the sea :( \n");
        System.out.print("Poor ");
        for (int i = AquariumFish.fishesList.size(); i > 0; i--){
            System.out.print(AquariumFish.fishesList.get(i-1).name + " ");
        }
        System.out.println("did not survive.");
        System.out.println("--------------------------------------");
        System.out.println("             THE END              ");
     
    }
    
    public static void main(String[] args) {

        AquariumFish fish0 = new AquariumFish(new String[]{
            "      /\\        ", // Argument for Fish Body
            "    _/./         ",
            " ,-'    `-:..-'/ ",
            ": o )      _  (  ",
            " `-....,--; `-.\\",
            "   `'            ",
            "                 ",
            "                 "
        }, "Ing"); // Argumentfor Fish Name
        AquariumFish fish1 = new AquariumFish(new String[]{
            "      /`·.¸         ",
            "     /¸...¸`:·      ",
            " ¸.·´  ¸   `·.¸.·´) ",
            ": © ):´;      ¸  {  ",
            " `·.¸ `·  ¸.·´\\`·¸)",
            "    `\\\\´´\\¸.·´   ",
            "                    ",
            "                    "
        }, "Groov");
        AquariumFish fish2 = new AquariumFish(new String[]{
            "    |\\    o   ", 
            "    |  \\    o ",
            "|\\ /    .\\ o ",
            "| |       (    ",
            "|/ \\     /    ",
            "    |  /       ",
            "    |/         ",
            "               "
        }, "In");
        AquariumFish fish3 = new AquariumFish(new String[]{
            "            ___             ",
            "         _ / __)_   °       ",
            " _     .'_'-'\\ /-'-. o °   ",
            "\\'-._.'-'\\ / _\\-(O)_: O  ",
            " \\ (__\\/_ \\ '._)  _\\ o  ",
            " /.' (_.'----''./'          ",
            "                '           ",
            "                            "
        }, "Jam");
        AquariumFish fish4 = new AquariumFish(new String[]{
            "   ____      ",
            "  /    \\    ",
            " /----./     ",
            "/ o    \\/|  ",
            ">        |   ",
            "\\ <)   /\\| ",
            " \\----'\\   ",
            "  \\____/    "
        }, "Ben");

        AquariumFish.print();
    }

}

AquariumFish.main(null);
The Sea Aquarium
5 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	            ___             	   ____      	
    _/./         	     /¸...¸`:·      	    |  \    o 	         _ / __)_   °       	  /    \    	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	 _     .'_'-'\ /-'-. o °   	 /----./     	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	\'-._.'-'\ / _\-(O)_: O  	/ o    \/|  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	 \ (__\/_ \ '._)  _\ o  	>        |   	
   `'            	    `\\´´\¸.·´   	    |  /       	 /.' (_.'----''./'          	\ <)   /\| 	
                 	                    	    |/         	                '           	 \----'\   	
                 	                    	               	                            	  \____/    	
A shark gobbled up Ben in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

4 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	            ___             	
    _/./         	     /¸...¸`:·      	    |  \    o 	         _ / __)_   °       	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	 _     .'_'-'\ /-'-. o °   	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	\'-._.'-'\ / _\-(O)_: O  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	 \ (__\/_ \ '._)  _\ o  	
   `'            	    `\\´´\¸.·´   	    |  /       	 /.' (_.'----''./'          	
                 	                    	    |/         	                '           	
                 	                    	               	                            	
A shark gobbled up Jam in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

3 Little fishies swimming in the sea...
      /\        	      /`·.¸         	    |\    o   	
    _/./         	     /¸...¸`:·      	    |  \    o 	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	|\ /    .\ o 	
: o )      _  (  	: © ):´;      ¸  {  	| |       (    	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	|/ \     /    	
   `'            	    `\\´´\¸.·´   	    |  /       	
                 	                    	    |/         	
                 	                    	               	
A shark gobbled up In in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

2 Little fishies swimming in the sea...
      /\        	      /`·.¸         	
    _/./         	     /¸...¸`:·      	
 ,-'    `-:..-'/ 	 ¸.·´  ¸   `·.¸.·´) 	
: o )      _  (  	: © ):´;      ¸  {  	
 `-....,--; `-.\	 `·.¸ `·  ¸.·´\`·¸)	
   `'            	    `\\´´\¸.·´   	
                 	                    	
                 	                    	
A shark gobbled up Groov in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

1 Little fishies swimming in the sea...
      /\        	
    _/./         	
 ,-'    `-:..-'/ 	
: o )      _  (  	
 `-....,--; `-.\	
   `'            	
                 	
                 	
A shark gobbled up Ing in a spree
The employee called the manager and the manager said, 
'No more sharks swimming in the sea!'

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

No more fishies living in the sea :( 

Poor Ben Jam In Groov Ing did not survive.
--------------------------------------
             THE END