Introduction to Java using NetBeans

Section 7 : Arrays

 

Arrays

An array is a special type of data structure that allows you to store multiple values in a single structure.

It is also a series of numbered memory locations with a shared variable name.

The most basic way to store information is by using a variable.

Arrays are groups of related variables; these variables are usually the same data type. Many programming languages require arrays values to be of the same data type. However, Java allows you to store different data types in the same array.

 

Using Multiple String Variables

// Until now we have used multiple variables to store elements
// This is not very efficient and requires lots of code

 

// Declaring multiple Strings

String strStudent0 = "Lisa";
String strStudent1 = "Maggie";
String strStudent2 = "Todd";
String strStudent3 = "Rod";
String strStudent4 = "Nick";
String strStudent5 = "Jaxon";
String strStudent6 = "Zoey";

// To print multiple Strings

System.out.println(strStudent0);
System.out.println(strStudent1);
System.out.println(strStudent2);
System.out.println(strStudent3);
System.out.println(strStudent4);
System.out.println(strStudent5);
System.out.println(strStudent6);

 

 

Tutorial 7. 1- Using Arrays (Console)

    In this program we will complete the following:

    •Declare an array with seven different names

    Use a for loop to obtain the values of the array from the console

    •Use a for loop to list the elements of the array in order (starting with the element with the lowest index number)

    •Use a for loop to list the elements of the array in reverse order (starting with the element with the highest index number)

 

Creating an Array

// Arrays allow you to use one variable with different positions
// This is more efficient and with the use of loops simplifies code

// Declare a new Array of type String with 7 positions
// Postitions start at 0


String [] strStudentArr = new String[7];

// Assign values to the string, indexes will go from 0 - 6

strStudentArr[0] = "Lisa";
strStudentArr[1] = "Maggie";
Etc…

Arrays help you to make organized, orderly collections of data quickly. You can see from the example above how the use of an array made the program code much easier to use and read

 

.


// Printing one element in an Array
System.out.println(strStudentArr[0]); // displays Lisa

 

//Printing Values using a For Loop

for (int i = 0; i < strStudentArr.length; i++)
{
System.out.println(i + ": " + strStudentArr[i]);
}

 


Putting Scanner Input into an Array

// Assigning Values from the Console



// Delare a scanner
Scanner input = new Scanner(System.in);

// Getting elements of the array from the console
for ( i= 0; i<strStudentArr.length; i++ )
{
// Print Message
System.out.println("Enter a name");
// Print element
strStudentArr[i]= input.nextLine();
}

 

 

 

Assignment 7.1 - Marks (Console)

Create a program that asks how many students you have and then uses a for loop to get their names and marks. Display the names and marks as well as the class average, the highest mark and the lowest mark.(Note: You may not use the sort function) Error check to make sure that the mark is between 0 and 100 and that it is an integer.

Hints: Use a for loop to calculate the highest mark. Create a highmark variable and initialize it to 0. Each time through the loop check if the current number > highmark. If it is set highmark to the current value.

Do the same thing for lowmark (use the same loop) but initialize lowmark to 100

 

Sorting Arrays

 

 

 

Assignment 7.2 - Random Stats (Console)

Create a RandomStats application that generates 200 random numbers between 1 and 100, and then sorts them. The program must make use of a second array of 5 counters to count how many numbers fall into which category: 1 to 20, 21 to 40, 41 to 60, 61 to 80, and 81 to 100. Use an if ......else if inside a for loop to add 1 to the Category everytime the number falls in the correct range. (You do not need to use &&) Using a for loop print a "star-graph" to represent how many of each number were in any given category. You must use loops to print the graph. Do this with a nested for loop instead of using separate loops.

int Numbers[] = new int[200]

int Count[] = new int[5]

Application output should look similar to:

 

Hints:

1. Use a for loop to assign the random numbers to the Numbers array

i.e. Numbers[i] = random number formula

2. In the for loop check the value of the element in the Numbers array and add one to the Count array depending on the numbers value.

i.e. if (Numbers[i] < = 20){
Count[0]+=1}

3. Print the Numbers array using a for loop.
Use an if to determine when to move on to the next line. (Note: you could use 10 if ... else ifs but it is more efficient to use one if and the mod (%) feature)

4. To print the stars use 5 for loops and the values of the Count array (Note: More efficent to use 1 for loop and change the index of count each time, i.e. nested for loops)