1 / 14

CSE 1341 Honors

CSE 1341 Honors. Professor Mark Fontenot Southern Methodist University Note Set 17. What if…. What if we needed to track the grades for 20 students, the GPAs for 10000 students, the balance for 10M checking accounts? one variable for each “thing” we need to track?

ikia
Download Presentation

CSE 1341 Honors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17

  2. What if… • What if we needed to track the grades for 20 students, the GPAs for 10000 students, the balance for 10M checking accounts? • one variable for each “thing” we need to track? • Technically, it would be possible to do it this way. • Ridiculously difficult • Extensibility? • Ability to perform a simple calculation on 10M accounts such as Pay interest might take 10M lines of code – one line for each calculation. • Making a deposit would require 10M if statements. • Simply impractical

  3. Arrays • Allow programmer to refer to more than one object of the same type by using one name/identifier and an index. • index can be a variable • lend themselves well to use with loops

  4. Declaration • Declaring an array • arrays are objects in Java • Create a reference variable to an array by using [] • instantiate an array object using new <dataType> [size of array] EXAMPLE: int [] grades; grades = new int[10]; OR int[] grades = new int[10]; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] grades

  5. Declaration • Size can be input by the user. Scanner s = new Scanner(System.in); int size = 0; int [] data; System.out.print(“Please enter the number of students: “); size = s.nextInt(); data = new int [size]; //and so on

  6. Characteristics of Arrays • Zero subscripted • first slot (called element) has index of zero. • last element has index of (size – 1) • Homogeneously Typed • Every element store in array has to be of the same data type • Cannot change size once instantiated • Can tell you their size using data member length • int [] data = new int[1001];System.out.println( data.length );//prints 1001

  7. Accessing Elements of Array • Use an index to access the elements. • can be a literal or variable int [] data = new int [20]; data[0] = 98; data[1] = 20; Literal int [] data = new int[20]; for (inti = 0; i < data.length; i++) data[i] = i + 2; variable

  8. Array Example //Declare each element with the square of its index. //e.g. [2] would contain 4, [3] would contain 9, etc. public static void main (String [] args) { int [] data = new int [20]; for (inti = 0; i < data.length; i++) data [i] = i * i; for (inti = 0; i < data.length; i++) System.out.println(“data[“ + i + ”] = “ + data[i]); }

  9. Breakout 1

  10. On the Board: Searching or an Element

  11. On the Board: Find Max Element

  12. Arrays of Object References: Review • Remember Student stu1;only declares a reference variable to a Student object. • no actual Student object yet. stu1 ?

  13. Arrays of Object References Student [] class = new Student[10]; Creates and array of 10 Student references. There are no student objects yet. for (inti = 0; i < class.length; i++) class[i] = new Student(); Instantiates a new object for each element of the array to point to.

  14. Arrays of Object References • Access public interface of object from array element just like normal. System.out.println(class[2].getName()); class[3].setGPA(3.99); Acts like a typical Student reference variable

More Related