|
|
|
|
||
|
|
Introduction to Arrays |
|
||
|
|
Introduction In previous programs we have created individual variables to store data. However, another way to store data is using what is called an array. An array is essentially a series of adjacent memory locations all of the same data type. By using arrays the data can be quickly accessed using various loop control structures. As with other data types arrays are declared and then created. You should keep in mind that arrays and Strings are based upon similar concepts but are in fact different objects. The following program shows the implementation of a small integer array. |
|
||
|
|
/** * ArraysOne.java * * WDS * Version 1.00 */ public class ArraysOne { public static
void main(String[] args) { //an integer
array declaration int[] intArray; //array instantiation intArray = new int[7]; for (int i = 0; i < 7; i++) { System.out.println
("intArray[" + i + "] = " + intArray[i]); } }//end method }//end class |
|
||
|
|
The declaration and instantiation can be combined as shown below where this time an array of twenty integers is created. Notice that when arrays are created the first element in the array is going to have as its index value zero. |
|
||
|
|
//declaraction and instantiation int[] intArray02 = new int[20]; for (int i = 0; i < 20; i++) { intArray02[i] = i * i; System.out.print("
" + intArray02[i]); } |
|
||
|
|
Java also allows a shortcut in which you can also declare and populate the array at the same time. The following creates an array of the days of the week and then prints them out. |
|
||
|
|
String[] days =
{"Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"}; for (int i = 0; i < 7; i++) { System.out.println(days[i]); } |
|
||
|
|
Procedure A. Experimenting with Arrays 1. Start a new application and create an array. 2. Modify the code to test what happens when you try to access index values in the array that are greater than the declared values. B. Arrays of Other Types 1. Add an object class from a previous assignment, e.g. boxes. Create an array of these objects. 2. In the previous GUI assignment create and array of buttons. |
|
||
|
|
Programming Assignment Write a program that creates a card class and shuffles a deck of cards. One of the methods of the card class should then deal the cards and return four hands to four players (13 cards to each as if they were playing hearts) The program should distinguish between suits and between number and face cards. Use the InputDialog and MessageDialog boxes to interact with the program. Try to keep the the client program to a minimum and have the majority of the work be done by the card class. Turn in a program listing your source code. Source code should use proper formatting. Also turn in a copy of your output as in previous assignments. This can be obtained by running the program and then using AltPrintScreen to copy the window of the output onto the clipboard. Then paste the contents of the clipboard into WordPad and print out the document. In addition to the source code, the output, also turn in the answer to the questions listed below. |
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|