Java Programming

 

         

 

ArrayPractice

 

public class ArrayPractice {
static int[] arrayName;
/**
* Instructions - carefully read through the comments in this program
* and uncomment the lines of code to see what they do. Use this code as examples
* as you work through section 6.1 in the Oracle curriculum.
*/
public static void main(String[] args) {
// Declaring arrays - there are several ways to declare arrays so choose the
// one that you feel most comfortable with and use it consistently.
// This next line creates an array of Strings called teamMembers2 that can hold 5 names.
String[] teamMembers2 = new String[5];
// String[] teamMembers;
// This next line repeats what was accomplished in the previous step
// but it is accomplished in two lines, the first line declares the array.
// The second line initializes the new array to hold 5 values.
String teamMembers3[];
teamMembers3 = new String[5];

// There are two parts to every array, the index which keeps track of where a value is stored
// and the actual value or data that is stored in the array at that index.
// Think of an array as a series of cubby holes that hold stuff, each cubby has an index
// starting at 0 so you can tell which cubby to look in for the stuff you want.
// The process of moving through an array one cubby at a time in known as iteration.


double[] myList; // preferred way to declare an array - type [ ] arrayName;
double myList2[]; // Same as previous line but not the preferred way to declare an array
int[] myList3; // creates an array of integer values

// Actually creating the memory for the list
myList3 = new int[5]; // sets all values to the default of 0

// step 1 and 2 can be combined into one statement:
String[] myList4 = new String[6];

// How to step through an array to print each value
// Create a for loop using the length of the list as the condition for exiting the loop
// What will this print out?

for (int index =0; index < myList3.length; index++)
System.out.println("The value found at index " + index + " is: " + myList3[index]);

// How to create a list and fill it with values at the same time.
// If you know what items you want in your array you can create it
//and fill it all in one step

int[] myList5 = {1, 2, 3, 4, 5, 6};
// copy the for list used to print up myList3 and have it print up myList5.
// What will the output be?



// initializing arrays - assigning values to the array
// This line of code creates an array of Strings called teamMembers
// and then assigns five names to that array.
String[] teamMembers = {"Dodge", "Newman", "Canning", "Henderson", "Stein"};

// Iterate through an array to print up the value for each index

for (int index =0; index < teamMembers.length; index++) // prints out all null
System.out.println(teamMembers[index]);

// filling an array after it has been created using the index value of the array.
// Which team member is found at index 2?
teamMembers2[0] = "LeBron";
teamMembers2[1] = "Bryant";
teamMembers2[2] = "Jordan";
teamMembers2[3] = "Curry";


System.out.println("The first team member is " + teamMembers2[0]);
System.out.println("The last team member is " + teamMembers2[2]);

// iterate through an array, go through the array and print the value stored at each index.
for (int index =0; index < teamMembers.length; index++)
System.out.println(teamMembers[index]);

// The following code results in an ArrayIndexOutOfBoundsException
// This is one of the most common errors when working with arrays
//
// for (int index =0; index < 20; index++)
// System.out.println(teamMembers[index]);


System.out.println("");
// Notice that these next lines of code print out five lines of output
// but the last value is null because teamMembers2[4] has no value assigned to it
// to assign a value to teamMemers2 just use the assignment operator =
// Uncomment out the following line and see how the output changes
// teamMembers2[4]="Durant";
for (int index =0; index < teamMembers2.length; index++)
System.out.println(teamMembers2[index]);

// Review these lines of code and use these examples to help with section 6
// We will talk about the following code later.

printArray(myList3);

}// end of main method

public static void printArray(int[] array) {
arrayName = array;
for (int index =0; index < arrayName.length; index++)
System.out.println(arrayName[index]);

}

}// end of class