Presentation is loading. Please wait.

Presentation is loading. Please wait.

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102."— Presentation transcript:

1 Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102 #22 2014T1

2 © Peter Andreae COMP 102 22:2 Menu More ArrayLists: Administration: Terms Test #2: Next Monday, 6-7pm (7-8 for people doing PHYS lab) Rooms:HM LT205, KK LT303, MC LT103 Topics: Loops, Files, Creating, defining and using Objects, Event Driven Input (GUIs),

3 © Peter Andreae COMP 102 22:3 Methods on ArrayList mylist.size() returns the number of items in mylist. Note contrast to array:.size() vs.length UI.printf(“The garden had %d flowers%n”, flowers.size()); mylist.add(item) adds the item at the end of the ArrayList balloons.add( new Balloon(x, y, color) ); mylist.add(index, item) inserts the item at position index ( 0.. size ) int pos = UI.askInt(“Where do you want ” +name); receptionOrder.add(pos, name); item must be of the right type 01 2 3 receptionOrder: “Jim” “Ann” 4 “Cary” “Bob”

4 © Peter Andreae COMP 102 22:4 if (first>=0 && first =0 && scnd<attackList.size() && first!=second){ Methods on ArrayList mylist.get(index) returns the item at position index (0.. size-1) equivalent to myArray[index] mylist.set(index, item) replaces the current value at position index with item returns the old value at index equivalent to myArray[index] = item index must be 0.. size-1 UI.print(“Which two units do you want to swap?”); int first = UI.nextInt(); int scnd = UI.nextInt(); String temp = attackList.get(first); attackList.set(first, attackList.get(scnd)); attackList.set(scnd, temp); attackList.set(first, attackList.set(scnd, attackList.get(first))); Or

5 © Peter Andreae COMP 102 22:5 More Actions on ArrayList mylist.isEmpty() returns true iff there are no items in mylist. // Make each unit advance, if there are any units if ( ! attackList.isEmpty() ) { for ( int i=0; i<attackList.size(); i++ ) { Unit unit = attackList.get(i); unit.checkPath(); unit.advance(3); } } mylist.clear() removes all values from the list. // Restart and clear the list of all elements. if ( button.equals(“Restart” )) { UI.clearGraphics(); flowers.clear(); }

6 © Peter Andreae COMP 102 22:6 More Actions on ArrayList mylist.contains(item) returns true iff the item is somewhere in mylist mylist.remove(item) removes the item, if it is present returns true iff item was removed // Respond to a “Remove Person” button String name = UI.askString(“Person to remove”); if (receptionOrder.contains(name)){ receptionOrder.remove(name); } else { UI.println(“That person is not in the reception order”); } if ( ! receptionOrder.remove(name) ){ UI.println(“That person is not in the reception order”); } first occurrence of item if item is at several places in the list Or

7 © Peter Andreae COMP 102 22:7 More Actions on ArrayList mylist.indexOf(item) returns the position of item in mylist returns -1 if the item is not present // Report position on waiting list String name = UI.askString(“Your name:”); int i = waitingList.indexOf(name); if (i == -1) { UI.println(“You are not on the waiting list”); } else { UI.printf(“You are number %d in order%n”, i); } mylist.remove(index) removes the item at position index (0.. size-1) returns the value that was removed // Remove every third unit from attackList for (int index=1; index<attackList.size(); index = index+3){ attackList.remove(index); }

8 © Peter Andreae COMP 102 22:8 ArrayLists of numbers/booleans ArrayLists can only hold objects! ArrayList myNums; ArrayList sizes = new ArrayList (); How do we store a list of numbers? (or boolean values)? Have to use “wrapper” classes to wrap up the numbers into objects. Double, Integer, Boolean Java will automatically wrap and unwrap them as needed. ArrayList myNums; ArrayList ageList = new ArrayList (); for (int age = 18; age <= 45; age++){ ageList.add(age) } double, int, long, float, boolean, etc are primitive values, not objects.

9 © Peter Andreae COMP 102 22:9 Using ArrayLists of numbers public void analyseNumbers(String fileName){ ArrayList nums = new ArrayList (); try { Scanner sc = new Scanner(new File(fileName)) while (sc.hasNextDouble()){ nums.add(sc.nextDouble()); } sc.close(); } catch(IOException e){UI.println(“Reading numbers failed: "+e);} double max = Double.NEGATIVE_INFINITY; for (int i=0; i<nums.size(); i++ ) { double num = nums.get(i); if ( num > max) { max = num; } UI.printf(“%.3f is the largest of %d numbers%n”, max, nums.size()); } Read numbers into ArrayList Step through numbers to find largest


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102."

Similar presentations


Ads by Google