/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package class_notes;
import java.util.Arrays;
import java.util.Random;
/**
*
* @author randycires
*/
public class Class_Notes {
/**
* @param args the command line arguments
*/
public static void main
(String[] args
) {
//---------------------------------------------------------
// Working with Arrays
//---------------------------------------------------------
System.
out.
println("---------------------------------------------------------");
System.
out.
println(" Working with Arrays ");
System.
out.
println("---------------------------------------------------------");
double[]values = new double [10];
//The above line creates an array of type double with length 10.
System.
out.
println("Essentially all this for loop does is print the values of the array values");
for (int i = 0; i<values.length; i++){
System.
out.
println("values["+i
+"]="+values
[i
]);
}
//----------------------------------
// Arrays Reference
//----------------------------------
System.
out.
println("---------------------------------------------------------");
System.
out.
println("These values are to show what referencing an array does:");
System.
out.
println("---------------------------------------------------------");
int []scores={10,9,3,5,8};
//You can create an array like this too. This format allows you to
//insert the elements you want in each index.
int[]otherScores=scores;
//This is a reference. I just set them equal to each other. If I change
//one I change both.
otherScores[0]=100;
//this just set both the first element of arrays: scores and otherScores
//to 100.
System.
out.
println("scores[0]="+scores
[0]);
System.
out.
println("otherScores[0]="+otherScores
[0]);
//----------------------------------
// Copying an Array
//----------------------------------
System.
out.
println("-----------------------------------");
System.
out.
println("Now copying an array is different");
System.
out.
println("-----------------------------------");
//this is the code for copying an array
otherScores
=Arrays.
copyOf(scores,scores.
length);
//Now my questions are:
/*// the copyOf function what are its parameters?
I understand that scores is essentially just copying the values in the array.
Is scores.length here to assure that the arrays are the same size?
Previously, otherScores was created as a reference to scores how does this copy impact the global scope?
By using "Arrays.copyOf" the previous reference to scores is replaced by becoming a copy.It's overwritten*/
otherScores[0]=200;
//Note you reassign otherScores[0] to 200 here.
//If you didn't the copy would return scores[0] and otherScores[0] as the same "100" value.
System.
out.
println("If coded properly this should return 100 and 200 respectively."
+"scores was left alone and otherScores was changed but, otherScores is still based on scores.");
System.
out.
println("scores[0]="+scores
[0]);
System.
out.
println("otherScores[0]="+otherScores
[0]);
System.
out.
println("Testing that a copy and not a reference was done:");
System.
out.
println("scores[1]="+scores
[1]);
System.
out.
println("otherScores[1]="+otherScores
[1]);
//Both values should be 9
//----------------------------------
// Adding elements of an Array
//----------------------------------
System.
out.
println("---------------------------------------------------------");
System.
out.
println(" Add Scores");
System.
out.
println("---------------------------------------------------------");
//This for loop should print out all the elements of the array "scores" like previously in the first example.
for (int i = 0; i<scores.length; i++){
System.
out.
println("scores["+i
+"]="+scores
[i
]);
}
//totalScore is a variable declared to hold the sum of all the elements in the array scores.
int totalScore=0;
for(int i=0; i<scores.length ;i++){
totalScore=totalScore+scores[i];
}
System.
out.
println("Total Score = " + totalScore
);
//----------------------------------
// The Enhanced for Loop
//----------------------------------
System.
out.
println("---------------------------------------------------------");
System.
out.
println(" The Enhanced For Loop");
System.
out.
println("---------------------------------------------------------");
int[]scores2={1,2,3,4,5,6,7,8};
System.
out.
println("Without using an enhanced For Loop:");
for(int i=0;i<scores2.length;i++){
System.
out.
println("scores2["+i
+"]="+scores2
[i
]);
}
System.
out.
println("---------------------------------------------------------");
System.
out.
println("Using an enhanced For Loop:");
int i=0;// why this int??
for(int currentInt:scores2){
System.
out.
println("scores2["+i
+"]="+currentInt
);
i++;// including this here subverts the zero's for the index.
}
//A good use of the for loop. This adds all the elements in the array scores2
int totalSum=0;
for(int currentInt:scores2){
totalSum=totalSum+currentInt;
}
System.
out.
println("\n \n totalSum="+totalSum
);
}
}