Arrays
Arrays
Arrays are data structures consisting of related data items of the same type using contiguous memory locations.
We’ll be using static arrays (stays the same use once created).
Array elements are numbered the same as C++ numbering scheme.
(0 ( n-1, where n is the array size.)
myarray[0]=7
myarray[1]=198
myarray[2]=-225
myarray[3]=-75
myarray[4]=4
myarray[5]=118
myarray[6]=-36
myarray[7]=94
Every array is Java knows its own length and maintains this information in a variable called length.
myarray.length
Arrays: Declaring and Allocating
Arrays are allocated dynamically with the new operator.
int myarray[] = new int[4];
or
int myarray[];
myarray = new int [4];
or
String palindrome[]= new String[50], reverse[]= new String[50];
or
double[] sal1,sal2; //array type and brackets can be used at the //beginning to indicate that all identifiers listed represent arrays.
Allocating arrays and initializing its elements:
Look p318 InitArray Program
Initializer Lists
int temp[] = {50,60,70,80,90,100};
array size is determined by the number of elements in the initializer list.
new is not required when using initializer lists.
Look p320 Use of initializer list
final is like const from C++
Examples of Arrays:
Summing the Elements of an Array p322
Histograms p323
***Elements as Counters p325 (RollDie)
Student Poll Program p326
Assignment: Frequency of Die Sums Program
Simulate the rolling of two dice. Use your roll() method. Sum the rolls (numbers should vary from 2 to 12). Your program should roll the die 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Display the complete frequency array in a dialog box or using System.out.print/println()
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.