Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Andro Max
Added: Nov 2, 2018 11:39 AM
Views: 9
Tags: no tags
Notes.java
  1. package class_notes;
  2. import java.util.Arrays;
  3. import java.util.Random;
  4.  
  5. /**
  6.  *
  7.  * @author randycires
  8.  */
  9. public class Class_Notes {
  10.  
  11.     /**
  12.      * @param args the command line arguments
  13.      */
  14.     public static void main(String[] args) {
  15.     //---------------------------------------------------------        
  16.     // Working with Arrays
  17.     //---------------------------------------------------------
  18.             System.out.println("---------------------------------------------------------");
  19.             System.out.println(" Working with Arrays ");
  20.             System.out.println("---------------------------------------------------------");
  21.         double[]values = new double [10];
  22.         //The above line creates an array of type double with length 10.
  23.         System.out.println("Essentially all this for loop does is print the values of the array values");
  24.         for (int i = 0; i<values.length; i++){
  25.             System.out.println("values["+i+"]="+values[i]);
  26.         }
  27.      
  28.            
  29.     //----------------------------------
  30.     // Arrays Reference  
  31.     //----------------------------------
  32.       System.out.println("\n");
  33.       System.out.println("---------------------------------------------------------");
  34.       System.out.println("These values are to show what referencing an array does:");
  35.       System.out.println("---------------------------------------------------------");
  36.  
  37.        
  38.        
  39.         int []scores={10,9,3,5,8};
  40.         //You can create an array like this too. This format allows you to
  41.         //insert the elements you want in each index.
  42.        
  43.         int[]otherScores=scores;
  44.         //This is a reference. I just set them equal to each other. If I change
  45.         //one I change both.
  46.        
  47.         otherScores[0]=100;
  48.         //this just set both the first element of arrays: scores and otherScores
  49.         //to 100.
  50.        
  51.         System.out.println("scores[0]="+scores[0]);
  52.         System.out.println("otherScores[0]="+otherScores[0]);
  53.        
  54.     //----------------------------------
  55.     // Copying an Array  
  56.     //----------------------------------
  57.         System.out.println("\n");
  58.         System.out.println("-----------------------------------");
  59.         System.out.println("Now copying an array is different");
  60.         System.out.println("-----------------------------------");
  61.         //this is the code for copying an array
  62.         otherScores=Arrays.copyOf(scores,scores.length);
  63.        
  64.         //Now my questions are:
  65.         /*// the copyOf function what are its parameters?
  66.         I understand that scores is essentially just copying the values in the array.
  67.         Is scores.length here to assure that the arrays are the same size?
  68.         Previously, otherScores was created as a reference to scores how does this copy impact the global scope?
  69.         By using "Arrays.copyOf" the previous reference to scores is replaced by becoming a copy.It's overwritten*/
  70.        
  71.         otherScores[0]=200;
  72.         //Note you reassign otherScores[0] to 200 here.
  73.         //If you didn't the copy would return scores[0] and otherScores[0] as the same "100" value.
  74.         System.out.println("If coded properly this should return 100 and 200 respectively."
  75.         +"scores was left alone and otherScores was changed but, otherScores is still based on scores.");
  76.         System.out.println("scores[0]="+scores[0]);
  77.         System.out.println("otherScores[0]="+otherScores[0]);
  78.         System.out.println("Testing that a copy and not a reference was done:");
  79.        
  80.         System.out.println("scores[1]="+scores[1]);
  81.         System.out.println("otherScores[1]="+otherScores[1]);
  82.         //Both values should be 9
  83.    
  84.     //----------------------------------
  85.     // Adding elements of an Array    
  86.     //----------------------------------
  87.         System.out.println("\n");
  88.         System.out.println("---------------------------------------------------------");
  89.         System.out.println(" Add Scores");
  90.         System.out.println("---------------------------------------------------------");
  91.        
  92.         //This for loop should print out all the elements of the array "scores" like previously in the first example.
  93.         for (int i = 0; i<scores.length; i++){
  94.         System.out.println("scores["+i+"]="+scores[i]);
  95.         }
  96.         //totalScore is a variable declared to hold the sum of all the elements in the array scores.
  97.         int totalScore=0;
  98.        
  99.         for(int i=0; i<scores.length ;i++){
  100.             totalScore=totalScore+scores[i];
  101.         }
  102.         System.out.println("Total Score = " + totalScore);
  103.            
  104.     //----------------------------------
  105.     // The Enhanced for Loop  
  106.     //----------------------------------
  107.         System.out.println("\n");
  108.         System.out.println("---------------------------------------------------------");
  109.         System.out.println(" The Enhanced For Loop");
  110.         System.out.println("---------------------------------------------------------");
  111.        
  112.         int[]scores2={1,2,3,4,5,6,7,8};
  113.         System.out.println("Without using an enhanced For Loop:");
  114.         for(int i=0;i<scores2.length;i++){
  115.             System.out.println("scores2["+i+"]="+scores2[i]);
  116.         }
  117.         System.out.println("---------------------------------------------------------");
  118.         System.out.println("Using an enhanced For Loop:");
  119.         int i=0;// why this int??
  120.         for(int currentInt:scores2){
  121.             System.out.println("scores2["+i+"]="+currentInt);
  122.             i++;// including this here subverts the zero's for the index.
  123.         }
  124.         //A good use of the for loop. This adds all the elements in the array scores2
  125.         int totalSum=0;
  126.         for(int currentInt:scores2){
  127.             totalSum=totalSum+currentInt;
  128.         }
  129.         System.out.println("\n \n totalSum="+totalSum);
  130.     }
  131.    
  132.            
  133.            
  134.  
  135.        
  136.        
  137.        
  138.        
  139. }