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 student0 = "Lisa";
String student1 = "Maggie";
String student2 = "Todd";
String student3 = "Rod";
String student4 = "Milhouse";
String student5 = "Tom";
String student6 = "Jerry";

// Printing multiple Strings

System.out.println(student0);
System.out.println (student1);
System.out.println (student2);
System.out.println (student3);
System.out.println (student4);
System.out.println (student5);
System.out.println (student6);

 

 

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[] Student = new String[7];

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

Student[0] = "Lisa";
Student[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

 

.


Assigning Values using a For Loop

for (int i = 0; i < Student.length; i++){

System.out.println(i + ": " + Student[i]);
}

 

Accessing Array Elements

 

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

// Printing all elements of an Array using a for loop

/

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

Notes

  • The length property e.g., student.length of the array class is used to count the number of elements that have been stored in the array.
  • The loop iterates from 0 to one less than the length of the array, because length is a count of the elements, not the greatest index value.

 

 

Practice Assignment 7. 1- Getting Started (Console)

    Write programming statements to complete the following:

    •Declare and initialize an array with seven different names

    •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)

    •Use a for loop to list the elements with even index numbers

    •Use a for loop to list the odd index numbers

 

Putting Scanner Input into an Array

 

Getting Information from an Array

 

 

Assignment 7.2 - 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.3 - Random Stats

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: