Starting with Arrays

Today, we started working with basic arrays in class. Before we got into them, we compared them to stacks. We discussed some of the applicable uses of stacks and how they are ideal for a queue where records must remain in the order they were received.

After the discussion reviewing stacks, I then compared them to a basic array. We discussed that while we can select and manipulate any index within an array, the array is a finite size. We discussed advantages and disadvantages of the two data structures. We then moved into practicing with some code.

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Alternate Way to Print the Array
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int i=0; i<myFirstArray.length; i++){
   total += myFirstArray[i];
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int i=1;i<myFirstArray.length; i++){
     if (myFirstArray[i] > max)max = myFirstArray[i];
   }
 System.out.println("Max is " + max);
 }
}

We started the notes by creating the array (line 10). We also discussed the difference between:

int[] myFirstArray = {100, 25, 63, 3, 12, 50, 85};

and

int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};

The first method is the preferred method for JAVA. The second method was added to the syntax to accommodate programmers who had worked in C++.

Lines 13 through 15 focus on how to display the contents of the array using the for loop..

On line 13, we have a for loop that will start iterating (counting) at 0 using the variable “i” to do the counting. The loop will continue to run until “i” is no longer less than the length of the array. After the loop is successfully run, “i” is incremented by 1.

On line 14, we have what will be executed in each iteration of the loop. The instruction says to print myFirstArray[i].

Remember, the first time through the loop, i = 0. So, line 14 will print index 0 of myFirstArray, which is the first record in the array. The second time through the loop, i = 1. At this point, it will print index 1 of myFirstArray, which is the second record in the array. This process will continue until the end of the array is reached.

Lines 19 through 21 focus on how to display the contents of the array using the foreach loop.

On line 19, we start with a for loop but the conditions are completely different. We declare an integer variable named “everyElement” and set it to have the value received from myFirstArray.

Line 20 prints the value of the variable “everyElement”.

The foreach loop runs through until each and every record has been touched.

Lines 25 through 29 discuss how to calculate the sum of the array.

On line 25, we declare an integer variable named “total” with an initial value of 0.

On line 26, we create a for loop identical to the one used on line 13. This will allow us to traverse the entire array.

On line 27, we take the variable “total” and add the currently selected index to it.

  • On the first pass, we have 0 + 100 = 100
  • On the second pass, we have 100 + 25 = 125
  • On the third pass, we have 125 + 63 = 188
  • On the fourth pass, we have 188 + 3 = 191
  • On the fifth pass, we have 191 + 12 = 203
  • On the sixth pass, we have 203 + 50 = 253
  • On the seventh and final pass, we have 253 + 85 = 338

This section could have been solved also using a foreach loop as outlined below:

int total = 0;
for(int everyElement: myFirstArray){
    total += everyElement;
}
System.out.println("Total is " + total);

Lines 32 through 35 cover how to sort an array.

Line 32 uses the sort function of the Arrays sub-library of the java.util library to sort the array. The sort function was imported on line 6.

Line 33 is a foreach loop (identical to the one seen on line 19.

Line 34 prints the array as it did on line 20.

This section could have been solved also using a traditional for loop as outlined below:

Arrays.sort(myFirstArray);
for(int i=0; i<myFirstArray.length; i++){
  System.out.print(myFirstArray[i] + " ");
}

Finally, in lines 39 to 43, we analyze how to locate the largest item in the array.

We start by declaring an integer variable named “max” and assign it the value of index 0 of the array. Since the array was just sorted on line 32, index 0 is now 3. As such, the variable “max” now has a value of 3.

Line 40 uses a slightly modified for loop as it starts iterating at 1 and not 0 as we did on line 13 and on line 26.

Line 41 uses a conditional if statement to check if the current iterated index of the array is greater than the variable “max”. If the statement is TRUE, the variable “max” is assigned the value of the iterated index. If is it FALSE, the variable “max” remains with its currently assigned value. This process repeats until the entire array is traversed.

As has been the case through all of this, you could use a foreach loop on this section of the problem as well. Here is what that code would look like with a foreach loop:

int max = myFirstArray[0];
  for (int everyElement: myFirstArray){
   if (everyElement > max)max = everyElement;
  }
System.out.println("Max is " + max);

Here is the code using traditional for loops:

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array with for Loop
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Print the Array with foreach Loop
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int i=0; i<myFirstArray.length; i++){
   total += myFirstArray[i];
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int i=1;i<myFirstArray.length; i++){
     if (myFirstArray[i] > max)max = myFirstArray[i];
   }
 System.out.println("Max is " + max);
 }
}

Here is the code use foreach loops:

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array with for Loop
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Print the Array with foreach Loop
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int everyElement: myFirstArray){
   total += everyElement;
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int everyElement: myFirstArray){
    if (everyElement > max)max = everyElement;
   }
 System.out.println("Max is " + max);
 }
}

As you can see, both result in the same number of lines of code. It would ultimately be programmer’s preference and customer request for which loop structure would be used.

Leave a Reply

Your email address will not be published. Required fields are marked *