DOC PREVIEW
OSU CSE 1223 - Arrays

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 1223 Introduction to Computer Programming in Java Chapter 6 Arrays 1 A New Problem Consider the following task Input N real numbers representing temperature measurements and compute the following the average minimum and maximum temperature the number of temperatures that are above the average and the number of temperatures that are below the average the median temperature 2 What s the Problem We need to store all the temperatures before we can perform some of the computation How many variables do we need What are we going to name them 3 The Solution Arrays An array is a sequence of variables of the same data type any type can be used e g int double boolean String etc Each variable in the array is called an element Array elements are accessed through an index their position in the array 4 An Example int N 5 double temperatures temperatures new double N Scanner in new Scanner System in int pos 0 while pos N temperatures pos in nextDouble pos pos 1 5 An Example int N 5 declare an array variable called temperatures whose double temperatures elements will be double variables temperatures new double N Scanner in new Scanner System in int pos 0 while pos N temperatures pos in nextDouble pos pos 1 6 An Example int N 5 declare an array variable called temperatures whose double temperatures elements will be double variables temperatures new double N create an actual array with N 5 elements of type double Scanner in new Scanner System in int pos 0 while pos N temperatures pos in nextDouble pos pos 1 7 An Example int N 5 declare an array variable called temperatures whose double temperatures elements will be double variables temperatures new double N create an actual array with N 5 elements of type double Scanner in new Scanner System in int pos 0 while pos N temperatures pos in nextDouble pos pos 1 access the element at position pos in array temperatures 8 Declaring Arrays Arrays are declared like normal variables with the addition of between type and name e g double temperatures int scores boolean answers String names The declaration does not specify the number of elements Declaring an array does not reserve allocate memory for the array elements 9 Allocating The Array To allocate memory space for the array elements we need to use the new operator e g temperatures new double 10 int numScores 8 scores new int numScores answers new boolean 4 names new String 5 new is followed by the type of the elements and the number of elements between The number of elements can be any expression that evaluates to a positive integer value 10 Accessing Array Elements Elements of an array are accessed by using the name of the array variable followed by the index position in the array of the element between e g temperatures 2 scores numScores 1 answers 0 names 3 The index can range between 0 and the number of elements in the array 1 11 Array Length Once we declare and allocate an array we can retrieve the array length the number of elements in the array from a variable called arrayName length e g temperatures length is 10 scores length is 8 answers length is 4 names length is 5 12 Your Turn Declare and allocate an array of 10 integers call it a 13 Your Turn cont Write a loop that initializes the 10 elements of the array to the integers 1 2 3 4 5 6 7 8 9 10 14 Your Turn cont Write a loop that outputs the elements of the array one per line 15 Your Turn cont Write a loop that outputs the elements of the array one per line in reverse order 16 Your Turn cont Write a piece of code that checks if the elements in an array of char form a palindrome 17 For Loops A different syntax for a familiar concept They work well with arrays for init test update for block 18 For Loops A different syntax for a familiar concept They work well with arrays for init test update for block init while test for block update 19 For Loops A different syntax for a familiar concept They work well with arrays boolean expression for init test update statements for block statement sequence init while test for block update 20 For Loops A different syntax for a familiar concept They work well with arrays boolean expression for init test update statements for block statement sequence init while test for block update 21 Example Rewrite the loop that initializes the 10 elements of an array to the integers 1 2 3 4 5 6 7 8 9 10 int i 0 while i 10 a i i 1 i 22 Example Rewrite the loop that initializes the 10 elements of an array to the integers 1 2 3 4 5 6 7 8 9 10 int i 0 while i 10 a i i 1 i for int i 0 i 10 i a i i 1 23 Your Turn Our Old Friend Write a for loop that given a String variable str and a character variable ch counts and outputs the number of occurrences of character ch in String str 24 Your Turn Again Write a program segment using for loops that given two integer variables width and height outputs a rectangle of s of the given width and height 25 Arrays And Methods Arrays can be passed as parameters to methods and can be returned by methods functions e g private private private private static static static static void printArray double a void clearArray int a int copyArray int a int indexOfSmallest int startIndex double t private static void sort double temps 26 Print Array Given an array of double output each element on a separate line in the order in which they occur in the array 27 Clear Array Given an array of int set each element of the array to 0 28 Arrays as Reference Types Arrays in Java are objects Array variables hold a reference to an array object Arrays have a reference type rather than a primitive type Variables with a reference type hold a reference to a memory location of an object of that type The value of the variable is just the memory address where the object is stored in memory This leads to some non intuitive behavior for variables with reference types including arrays 29 Reference types vs Primitive Types What do you expect this segment of code to do int i 12 int j i j j 1 System out println i System out println j 30 Reference types vs Primitive Types What do you expect this segment of code to do int i 12 int j i j j 1 System out println i reports 12 System out println j reports 13 31 Reference types Now what do you expect this segment of code to do int a1 new int 2 a1 0 2 int a2 a1 a2 0 6 System out println a1 0 System out println a2 0 32 Reference types Now what do …


View Full Document

OSU CSE 1223 - Arrays

Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?