Rahmadnet

Documentation Comment

Jul 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package Arrays;
  2.  
  3. /**
  4. * <h1>Java Array Testing</h1>
  5. * This Program is just for testing java Arrays
  6. * and printing array of Objects
  7. * @author  Rahmad Nasution
  8. * @version 0.1
  9. * @since   2019-07-19
  10. */
  11.  
  12. /**
  13.  * This example explains how to create and use single and
  14.  * multidimensional arrays
  15.  */
  16.  
  17. public class JavaArrays {
  18.  
  19.   public static void main(String args[]) {
  20.    
  21.    /*
  22.     *create an array of primitives - 10 integers
  23.     *Since an array is an object we have to instantiate
  24.     *it with the new keyword
  25.     */
  26.     int iArr[] = new int[10];
  27.    
  28.      /*iterating across the array using a traditional for loop
  29.       * notice how we get the length of the array
  30.       */
  31.     for (int i = 0; i < iArr.length; i++) {
  32.       // Assign a value to the i th location of the array
  33.       iArr[i] = i * 100;
  34.     }
  35.     printArray(iArr);
  36.  
  37.      /*create an array of floats
  38.       * Here we create and populate the array at the same time
  39.       */
  40.     float[] fArr = { 1.2f, 3.4f, 4.5f, 5.6f };    
  41.     printArray(fArr);
  42.  
  43.      /*Let's create an array of String objects
  44.       *When creating an array of objects, we need to instantiate the array
  45.       *object as well as the Objects which will be held in the array
  46.       *We first create an array capable of holding 10 Strings
  47.       */
  48.     String stringArr[] = new String[10];
  49.    
  50.     // Now let us create the Strings to put in the array
  51.     for (int i = 0; i < 10; i++) {
  52.       /*notice that we had to instantiate the array object and
  53.        * each String object which we put into the array as well.
  54.        */
  55.       stringArr[i] = new String(String.valueOf(i));
  56.     }    
  57.     printArray(stringArr);
  58.  
  59.     // Now let's create multidimensional arrays
  60.     int iMultiDim[][] = new int[3][3];
  61.     for (int d1 = 0; d1 < 3; d1++) {
  62.       for (int d2 = 0; d2 < 3; d2++) {
  63.         iMultiDim[d1][d2] = d1 * d2;
  64.       }
  65.     }
  66.     printArray(iMultiDim);
  67.   }
  68.  
  69.   private static void printArray(int array[]) {
  70.     // printing array of integers
  71.     // Here we will use the enhanced for loop
  72.     for (int i : array) {
  73.       System.out.print(i + " ");
  74.     }
  75.     System.out.println("");
  76.   }
  77.  
  78.   private static void printArray(float array[]) {
  79.     // printing array of floats
  80.     for (float f : array) {
  81.       System.out.print(f + " ");
  82.     }
  83.     System.out.println("");
  84.   }
  85.  
  86.   private static void printArray(Object array[]) {
  87.     // printing array of Objects
  88.     for (Object f : array) {
  89.       System.out.print(f + " ");
  90.     }
  91.     System.out.println("");
  92.   }
  93.  
  94.   private static void printArray(int mDimArray[][]) {
  95.     for (int i = 0; i < mDimArray.length; i++) {
  96.       for (int j = 0; j < mDimArray[i].length; j++) {
  97.         System.out.print(mDimArray[i][j]);
  98.       }
  99.       System.out.println("");
  100.     }
  101.   }
  102. }
Add Comment
Please, Sign In to add comment