# Arrays

## Arrays

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Arrays CSC 171 FALL 2002 LECTURE 20

2. Arrays • Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade? 65.2 81.7 31.3 95.4 < - highest grade 76.1 58.6

3. Individual data items • If we knew that there were always 150 students in the class, we could store the data as individual variables • score1,score2,score3, . . , score150 • What would the data entry code look like? • What would the printing code look like? • How about finding the max?

4. Arrays • An array is a collection of data items of the same type • Every element of the collection can be accessed separately.

5. Constructing Arrays double [] data = new double[10];

6. double [] data = new double[10]; int x = data.length ; //instance field on arrays, x = = 10

7. Setting Array values • To get values into an array you need to specify which slot you want to use. • Specification is done with the [ ] operator • The [ ] operator follows the name of the array • The [ ] operator encloses and integer-valued expression called the index or subscript

8. Setting array values data[4] = 29.95; 0 1 2 3 4 5 6 7 8 9

9. Using array values • Similar to setting int i = 4 ; System.out.println(“data[“+i+”] ==“+ data[i]); > data[4] ==29.95

10. You access array elements with and integer position number, called the _______________, using the notation _______________.

11. You access array elements with and integer position number, called the ___index_______, using the notation ____a[index]___________.

12. Array data items • Suppose we want to write a program that reads a set of test grades and prints them, marking the highest grade? • What would the data entry code look like? • What would the printing code look like? • How about finding the max?

13. Array issues • Does it work? double [] data = new double[10]; data[10] = 5.4;

14. Array issues • Does it work? double [] data = new double[10]; data[10] = 5.4; When the program runs, an out-of-bounds subscript generates an exception and terminates the program – why?

15. Position numbers of an array range from _________ to ____________.

16. Position numbers of an array range from ___0_______ to ____a.length -1_________.

17. Use the ___________________field to find the number of elements in an array.

18. Use the _____length______field to find the number of elements in an array.

19. Array issues • Can we search for the top grade as follows double maxScore = data[0]; for (int i = 1;I<=data.length;i++) If (data[i] > maxScore) maxScore = data[i];

20. Array issues • An array of length n has index values from 0 to (n-1) double maxScore = data[0]; for (int i = 1;I<data.length;i++) If (data[i] > maxScore) maxScore = data[i];

21. Array issues • Does it work? public static void main(String[] args){ double[] data; If (data[0] > 4.0) System.out.println(“GT 4!”); }

22. Array issues • Arrays must be allocated! public static void main(String[] args){ double[] data = new double[10]; If (data[0] > 4.0) System.out.println(“GT 4!”); }

23. Array issues • Arrays can be initialized! public static void main(String[] args){ double[] data = {2,3,4,5,6}; If (data[0] > 4.0) System.out.println(“GT 4!”); } // note: new int[] {2,3,4,5,6} ; is also legal

24. Copying Arrays Is this ok? double [] data = new double[10]; double[] testScores; testScores = data;

25. Copying Arrays Is this ok? double [] data = new double[10]; double[] testScores; testScores = data; How many arrays are there?

26. Copying Array REFERENCES double [] data = new double[10]; double[] testScores; testScores = data;

27. Copying Arrays • So, what if we want to make two “real” copies – what does the code look like? • Write a method so that int[] x = {3,4,5,6,7,8,9,10}; int[] y = myCopy(x);

28. Copying public static int[] myCopy(int[] x){ int[] r_arry = new int[x.length]; for (int i = 0 ; i< x.length;i++) r_arry[i] = x[i]; return r_arry; }

29. System.arrayCopy //System.arraycopy(from,fromstart,to,toStart,count); System.arraycopy(data,0,prices,0,data.length);

30. Clone

31. An array variable stores a ________________________. Copying the variable yields a second ____________________ to _________________ array.

32. An array variable stores a ______reference__________. Copying the variable yields a second ______reference______________ to ____the same_____________ array.

33. Use the _____________________________ method to copy elements from one array to another.

34. Use the _System.arraycopy____________________ method to copy elements from one array to another.

35. If you run out of space in an array you need to _______________ a larger array and copy the elements into it.

36. If you run out of space in an array you need to __allocate_____ a larger array and copy the elements into it.

37. Getting a Bigger Array

38. More Arrays Suppose we want to write a program that reads a set of test product names, prices, and quality scores prints them, marking the best value? (score/prices) Digital 500X, \$3499.00, score 73 ZEOS Pentium-III/500, \$2545.00, score 70 Micro Express MF, \$2195.00, score 72 < - best value Polywell Poly 450IP, \$2099.00, score 30

39. Parallel Arrays • One solution, can you think of a better one?

40. Arrays of Objects • Easier to deal with - arrays hold references

41. Avoid ___________________________ arrays by changing them into arrays of objects.

42. Avoid ____________parallel_______ arrays by changing them into arrays of objects.

43. Multidimensional Arrays • Arrays of arrays • Arrays are objects • Arrays hold references to objects • Ergo, arrays can hold arrays

44. Tables are 2D arrays // how easy to modify code ???? int[][] mtable = new int[5][5]; for(i=0;i<5;i++) • for(int j=0;j<5;j++) mtable[i][j] = (i+1)*(j+1); int size = 5; int[][] mtable = new int[size][size]; for(i=0;i<size;i++) for(int j=0;j<size;j++) mtable[i][j] = (i+1)*(j+1);