Thursday, February 13, 2014

Java Programming: 2D Arrays and for-loops

Here is a simple Java program that uses an array to store Names and Addresses:
----------------------------------------------------------------------------------------------------
class NamesAndAddresses{

  public static void main(String args[])
  {        


    // a 2-D string array with four rows and four colomns.
   
    String[][] Names = new String[][]

            {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
              { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
              {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
              {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};
   
    // A for loop for printing the elements in order 


    System.out.println("\nPHONE BOOK: \n");       


    for (int i=0;i <(Names.length);i++)
          System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                            "  email: " + Names[i][2] + "  phone: " + Names[i][3]);
  }
}

-----------------------------------------------------------------------------------------------
Let's look at this one statement at a time:
The String array "Names" has 4 rows and 4 colomns. 
Each row corresponds to one entry.
---------------------------------------------------------------------------------------------

String[][] Names = new String[][]
         {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
          { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
           {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
           {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};


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

Here is a "for loop" that I use for printing the array.
--------------------------------------------------------------------------------------------------

System.out.println("\nPHONE BOOK: \n");  

for (int i=0;i <(Names.length);i++)
      System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                     "  email: " + Names[i][2] + "  phone: " + Names[i][3]);

 --------------------------------------------------------------------------------------------------
 When I compile and run the program,  the resulting output on my console is:
---------------------------------------------------------------------------------------------------

PHONE BOOK:

 Name: suchita  Address:  1234 Dallas TX  email: suchita@yahoo.com  phone: 512 567 8904
 Name: Maria  Address: 345 Houston TX  email: maria@gmail.com  phone: 456 890 2345
 Name: Ben  Address: 789 Boston MA  email: ben@tt.com  phone: 345 456 7890
 Name: John  Address: 890 San Francisco CA  email: john@pinterest.com  phone: 234 789 2415


--------------------------------------------------------------------------------------------------
 Here is a "for loop" for printing the array in reverse:
---------------------------------------------------------------------------------------------------

System.out.println("\nPHONE BOOK IN REVERSE: \n");

    for (int i=Names.length-1 ;i >=0 ;i--)
      System.out.println(" Name: " + Names[i][0] + "  Address: " + Names[i][1]+

                     "  email: " + Names[i][2] + "  phone: " + Names[i][3]);
 ----------------------------------------------------------------------------------------------------
 When I compile and run the program,  the resulting output on my console is:
-----------------------------------------------------------------------------------------------------

PHONE BOOK IN REVERSE:

 Name: John  Address: 890 San Francisco CA  email: john@pinterest.com  phone: 234 789 2415
 Name: Ben  Address: 789 Boston MA  email: ben@tt.com  phone: 345 456 7890
 Name: Maria  Address: 345 Houston TX  email: maria@gmail.com  phone: 456 890 2345
 Name: suchita  Address:  1234 Dallas TX  email: suchita@yahoo.com  phone: 512 567 8904

------------------------------------------------------------------------------------------------------
Here is the program with a nested for loop for printing out the array:
------------------------------------------------------------------------------------------------------
 class MultiDArray{
  public static void main(String args[])
  {      
    // a 2-D string array with four rows and four colomns.
  
    String myArray[][] = new String[][]

               {{"suchita"," 1234 Austin TX", "suchita@yahoo.com","512 567 8904"},
                 { "Maria","345 Houston TX", "maria@gmail.com", "456 890 2345"},
                 {"Ben","789 Boston MA", "ben@tt.com","345 456 7890"},
                  {"John","890 San Francisco CA","john@pinterest.com", "234 789 2415"}};
  
    String display[] = new String[]{" Name: ", " Address: ", " Email:", " Phone :"};
  
    // A for loop for printing out the elements in order
    System.out.println("\nPHONE BOOK: \n");     
    for (int i=0;i < (myArray.length);i++)
    {
      for (int j=0; j< myArray[i].length; j++){
        System.out.println(display[j] + myArray[i][j]);
      }
      System.out.println();
    }
  }
}

------------------------------------------------------------------------------------------------------
Here is a phone book array that is created and then the entries filled in at
 the console by the user.
------------------------------------------------------------------------------------------------------

import java.util.Scanner;

class NamesAndAddresses{

  public static void main(String args[])
  {        


    Scanner scan = new Scanner( System.in );

    String[][] UserInput = new String[4][4];  


    for (int i=0;i < (UserInput.length);i++)
    {
         System.out.println(" Enter Name: ");
         UserInput[i][0]=scan.nextLine();
         System.out.println(" Enter Street Address: ");
         UserInput[i][1]=scan.nextLine();
         System.out.println(" Enter email address: ");
         UserInput[i][2]=scan.nextLine();
         System.out.println(" Enter phone number: ");
         UserInput[i][3]=scan.nextLine();
    }


  // A for loop for printing the elements in order 

 
    System.out.println("\nPHONE BOOK: \n");      
    for (int i=0;i < (UserInput.length);i++)
      System.out.println(" Name: " + UserInput[i][0] +

                                  "  Address: " +  UserInput[i][1]+
                                  "  email: "  + UserInput[i][2] +
                                  "  phone: " + UserInput[i][3]);
   
     // A for loop for printing the elements in reverse order 

 
    System.out.println("\nPHONE BOOK IN REVERSE: \n");
    for (int i=UserInput.length-1 ;i >=0 ;i--)
      System.out.println(" Name: " + UserInput[i][0] +

                                   "  Address: " + UserInput[i][1]+
                                     "  email: "   + UserInput[i][2] + 
                                     "  phone: " + UserInput[i][3]);  
}
}

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


No comments:

Post a Comment