CSIS 110 - Lecture 14: For/While Loop, ArrayLists, and Arrays - Prof. Brian F. Hanks, Study notes of Javascript programming

Announcements for csis 110 lecture 14, including a quiz on for and while loops, the relationship between them, and when to use each. The document also discusses removing all items from a list, finding a particular student, and the differences between arraylists and arrays. Students are encouraged to write code and email the results for lab exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-8vo-1
koofers-user-8vo-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 14
Announcements
Quiz – Wednesday, Feb 15. Continue reviewing chapter 4. Homework due Friday.
For/While Loop Relationship
How can you convert one to the other? Let's look at the parts, see how they relate to each
other
What are the ‘rules’ for deciding whether to use a for loop or a while loop?
- For loop: when you know ahead of time how many iterations there are.
- While loop: when you just keep going until some condition is true
Examples: Given a sorted array of ints: int[] myArray;
Use a for or while?
- to display all the values in the array
- to find the first element whose value is larger than 15?
- to display the last 5 elements of the array?
- to calculate the sum of the elements?
- to search for an element with the value 27?
Removing all items from a list
Let's look at some different ways for removing all the items from a list.
Load the student2 project
Note that in this version
Students have a studentID
I have added a buildStudentBody method that creates a bunch of students
There is a printAllStudents method that prints all of the students in the
studentBody
Try to write a method that removes all of the elements in a list. [Copy the
printAllStudents method an modify it to call remove instead – Analyze why this doesn’t
work]
Look at alternative options
1) delete from the rear
2) always delete the front item – discuss why this version doesn’t have an obvious
‘prepare for next iteration’ part of the loop
pf3

Partial preview of the text

Download CSIS 110 - Lecture 14: For/While Loop, ArrayLists, and Arrays - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 14

Announcements Quiz – Wednesday, Feb 15. Continue reviewing chapter 4. Homework due Friday. For/While Loop Relationship How can you convert one to the other? Let's look at the parts, see how they relate to each other What are the ‘rules’ for deciding whether to use a for loop or a while loop?

  • For loop: when you know ahead of time how many iterations there are.
  • While loop: when you just keep going until some condition is true Examples: Given a sorted array of ints: int[] myArray; Use a for or while?
  • to display all the values in the array
  • to find the first element whose value is larger than 15?
  • to display the last 5 elements of the array?
  • to calculate the sum of the elements?
  • to search for an element with the value 27? Removing all items from a list Let's look at some different ways for removing all the items from a list. Load the student2 project Note that in this version  Students have a studentID  I have added a buildStudentBody method that creates a bunch of students  There is a printAllStudents method that prints all of the students in the studentBody Try to write a method that removes all of the elements in a list. [Copy the printAllStudents method an modify it to call remove instead – Analyze why this doesn’t work] Look at alternative options
  1. delete from the rear
  2. always delete the front item – discuss why this version doesn’t have an obvious ‘prepare for next iteration’ part of the loop

Finding a particular student Let’s write a method public void printStudent( String studentID ) that uses the studentID to find a student’s record an print it. [Write pseudo-code first. Then try to write loop. What if student not found? What should we do when we find the student?] ArrayLists and arrays ArrayLists are variable size, arrays are fixed ArrayLists store objects, arrays can store values of any type (including primitives like int) Declaration: ArrayList myList; String[] myArray; Creation: myList = new ArrayList(); myArray = new String[ 10 ]; Note that ArrayList is empty, array has 10 elements whose values are null. We haven't put any Strings in either one yet. Accessing Elements: myList.get(0) myArray[0] Number of elements: myList.size() -- with parens myArray.length -- no parens! Important – ArrayList size() method tells us how many items are int the list. array.length field tells us how many elements are in the array, but does not tell us how many of them we are using! If our application does not use all of the array elements, we need to keep track of the size separately. Storing a value: String s = "Happy Birthday"; myList.add( s ); // puts at the end myArray[ index ] = s; // puts at position index