ArrayOutOfBoundsException_第1頁(yè)
ArrayOutOfBoundsException_第2頁(yè)
ArrayOutOfBoundsException_第3頁(yè)
ArrayOutOfBoundsException_第4頁(yè)
ArrayOutOfBoundsException_第5頁(yè)
已閱讀5頁(yè),還剩21頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Chapter 10Arrays 數(shù)組OutlinesArraysFor loopsRandom numbers 隨機(jī)數(shù)Counting and histogram 柱狀圖ArraysAn array is a set of values where each value is identified by an index, all the values in an array have to have the same type.Declare variables : int count; double values; Until you initialize these variables

2、, they are set to null. To create the array itself, use the new command: count = new int4; values = new doublesize; 10.1 Accessing elements To store values in the array, use the operator. count0 = 7; count1 = count0 * 2; count2+; count3 -= 60; count0 refers to the zeroeth element of the array, and c

3、ount1 refers to the oneth element.It is a common error to go beyond the bounds of an array, which will cause an ArrayOutOfBoundsException. One of the most common ways to index an array is with a loop variable i: int i = 0; while (i 4) System.out.println (counti); i+; Each time through the loop we us

4、e i as an index into the array, printing the ith element. This type of array traversal is very common.10.2 Copying arraysWhen you copy an array variable, remember that you are copying a reference to the array: double a = new double 3; double b = a; This code creates one array of three doubles, and s

5、ets two different variables to refer to it. This situation is a form of aliasing. Any changes in either array will be reflected in the other.You can make a copy of the array, by allocating a new array and copying each element from one to the other: double b = new double 3; int i = 0; while (i 4) bi

6、= ai; i+; OutlinesArraysFor loopsRandom numbersCounting and histogram for (INITIALIZER; CONDITION; INCREMENTOR) BODY is exactly equivalent to INITIALIZER; while (CONDITION) BODY INCREMENTOR it is more concise and, since it puts all the loop-related statements in one place, it is easier to read. For

7、example: for (int i = 0; i 4; i+) System.out.println (counti); is equivalent to int i = 0; while (i 4) System.out.println (counti); i+; 10.3 for loops10.4 Arrays and objectsIn many ways, arrays behave like objects:When you declare an array variable, you get a reference to an array. You have to use t

8、he new command to create the array itself. When you pass an array as an argument, you pass a reference, which means that the invoked method can change the contents of the array. Differences:The elements of an array are identified by indices, whereas the elements (instance variables) of an object hav

9、e names (like x, width, etc.).All the elements of an array have to be the same type. Some objects have instance variables with different types (like Time).10.5 Array lengthActually, arrays do have one named instance variable: length. for (int i = 0; i a.length; i+) bi = ai; The last time the body of

10、 the loop gets executed, i is a.length - 1, which is the index of the last element. OutlinesArraysFor loopsRandom numbersCounting and histogram10.6 Random numbersJava provides a built-in method that generates pseudorandom numbers, which are not truly random in the mathematical sense, but for our pur

11、poses, they will do.The random method in the Math class. Its return value is a double between 0.0 and 1.0. for (int i = 0; i 10; i+) double x = Math.random (); System.out.println (x); 10.7 StatisticsThe numbers generated by random are supposed to be distributed uniformly. that means that if we divid

12、e the range of possible values into equal sized buckets, and count the number of times a random value falls in each bucket, each bucket should get the same number of hits (eventually).In the next few sections, we will write programs that generate a sequence of random numbers and check whether this p

13、roperty holds true.10.8 Array of random numbersTo generate: public static double randomArray (int n) double a = new doublen; for (int i = 0; ia.length; i+) ai = Math.random (); return a; To print: public static void printArray (double a) for (int i = 0; ia.length; i+) System.out.println (ai); The fo

14、llowing code generates an array and prints it: int numValues = 8; double array = randomArray (numValues); printArray (array); OutlinesArraysFor loopsRandom numbersCounting and histogram10.9 CountingAn example of a pattern called traverse and count. The elements of this pattern are:A set or container

15、 that can be traversed, like an array or a string. A test that you can apply to each element in the container. A counter that keeps track of how many elements pass the test. public static int inBucket (double a, double low, double high) int count = 0; for (int i=0; i= low & ai high) count+; return c

16、ount; The parameters are the array and two doubles that specify the lower and upper bounds of the bucket.To divide it into four pieces: int bucket1 = inBucket (a, 0.0, 0.25); int bucket2 = inBucket (a, 0.25, 0.5); int bucket3 = inBucket (a, 0.5, 0.75); int bucket4 = inBucket (a, 0.75, 1.0); 10.10 Ma

17、ny bucketsWe can solve the first problem with a loop: int numBuckets = 8; double bucketWidth = 1.0 / numBuckets; for (int i = 0; i numBuckets; i+) double low = i * bucketWidth; double high = low + bucketWidth; System.out.println (low + to + high); output of this loop is: 0.0 to 0.125 0.125 to 0.25 0

18、.25 to 0.375 0.375 to 0.5 0.5 to 0.625 0.625 to 0.75 0.75 to 0.875 0.875 to 1.0 You can confirm that each bucket is the same width, that they dont overlap, and that they cover the whole range from 0.0 to 1.0.well invoke inBucket and store the result: int numBuckets = 8; int buckets = new int 8; doub

19、le bucketWidth = 1.0 / numBuckets; for (int i = 0; inumBuckets; i+) double low = i * bucketWidth; double high = low + bucketWidth; /System.out.println (low + to + high); bucketsi = inBucket (a, low, high); The output is: 129 109 142 118 131 124 121 126 which is pretty close to 125 in each bucket. 10

20、.11 A single-pass solutionAlthough the previous code works, it is not as efficient as it could be. Every time it invokes inBucket, it traverses the entire array. As the number of buckets increases, that gets to be a lot of traversals.It would be better to make a single pass through the array, and fo

21、r each value, compute which bucket it falls in. Then we could increment the appropriate counter. int numBuckets = 8; int buckets = new int 8; for (int i = 0; i numValues; i+) int index = (int) (ai * numBuckets); bucketsindex+; Histogram 柱狀圖 An array like buckets, that contains counts of the number of values in each range, is called a histogram.OutlinesArraysFor loopsRandom numbersCounting and histogramSummaryarray A named collection of values, where all the values have the same type, and each value is identified by an index. count = new int4; count0 = 7; c

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論