Suggest a feature
×

Welcome to TagMyCode

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