1 / 23

IAT 800

IAT 800. ArrayList, Text, Java Libraries. Topics. ArrayLists ArrayLists + Polygons Some words on Strings. ArrayLists. Pros: Are great if you don’t know how many things are going to go into the array. Automatically resizes when you add to a full ArrayList. Cons:

mattox
Download Presentation

IAT 800

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. IAT 800 ArrayList, Text, Java Libraries IAT 800

  2. Topics • ArrayLists • ArrayLists + Polygons • Some words on Strings IAT 800

  3. ArrayLists • Pros: • Are great if you don’t know how many things are going to go into the array. • Automatically resizes when you add to a full ArrayList. • Cons: • Only stores objects, not primitive variables (int, float…). Need to make objects from those. • Only returns things of the Object type. Need to explicitly cast back to the type you put in there. IAT 800

  4. ArrayLists • Remember, an ArrayList is just like an array: If we decide we need to add another element to the array, we need to make a NEW, bigger array, and copy all the elements into it. What a pain! IAT 800

  5. ArrayLists • ArrayLists do this for us! We don’t even have to declare a size when we first create it, only deciding how many elements we’ll have when we actually add them. • ArrayList a = new ArrayList(); • a.add(Object o) – adds an object at next index. • a.get(int i) – returns object at i index. • a.size() – returns number of items in the ArrayList. IAT 800

  6. Simple Example - ArrayList • Let’s make a little class called Point, which simply stores an X and Y value associated with a point on the screen. class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } } IAT 800

  7. Simple Example - ArrayList • ArrayList of Points instead of two arrays of x and y points. ArrayList pointList = new ArrayList(); pointList.add(new Point(45, 50)); pointList.add(new Point(79, 23)); // let’s draw a circle at our second point. Point p2 = (Point)pointList.get(1); ellipse(p2.x, p2.y, 50, 50); IAT 800

  8. Simple Example - ArrayList • We could extend this so that every time we click, that point gets added to our ArrayList. void mouseReleased() { pointList.add(new Point(mouseX, mouseY)); } IAT 800

  9. Simple Example - ArrayList • We could then do anything with these points as they accumulate. Here’s an example: void draw() { background(255); if(pointList.size() > 1) { beginShape(); for(int i = 0; i < pointList.size(); i++) { Point p1 = (Point)pointList.get(i); vertex(p1.x, p1.y); } endShape(CLOSE); } } Every time we click, the polygon will re-draw, using the new point we’ve added. IAT 800

  10. Types • You may recall when we talked about types • Primitives • int, float, byte • boolean • char • Objects (composites) • Array • ArrayList • PImage • (any object you create) • Strings IAT 800

  11. String details • A string is almost like an array of chars • char someletter = 'b'; • String somewords = "Howdy-do, mr. jones?"; • Note the use of double-quotes (vs. apostrophes) • Like the objects we've created with classes, it has several methods, too… IAT 800

  12. String methods • From http://processing.org/reference/String.html • length() • returns the size of the String (number of letters) • charAt(number) • returns the char at an index number • toUpperCase() and toLowerCase() • returns a copy of the String in UPPERCASE or lowercase respectively. • substring(beginIndex, endIndex) • returns a portion of the String from beginIndex to endIndex-1 String howdy = "Hello!"; String expletive = howdy.substring(0,4); IAT 800

  13. String concatenation • Concatenation is just a fancy word for slapping together to make one! • With Strings, this is done using the + symbol • So, if you have: • You'll get out: String s1 = "She is the "; String s2 = "programmer.“ ; String sentence = s1 + "awesomest " + s2; println(sentence); // sentence == "She is the awesomest programmer." // outputs: She is the awesomest programmer. IAT 800

  14. MORE String concatenation • You can also add in numbers, too! • There is also a function called nf() which can format your numbers (it stands for number format) • It has siblings! nfs(); nfp(); nfc(); Consult the reference. String anothersentence = s1 + "#"+ 2 + " " + s2;// "She is the #2 programmer." anothersentence = s1 + nf(7,3) + " " + s2;// nf( integer, number of digits )// "She is the 007 programmer." anothersentence = s1 + nf(3.14159,3,2) + " " + s2;// nf( float, digits before decimal, digits after decimal )// "She is the 003.14 programmer." IAT 800

  15. Strings and Arrays • Did you know that you can take an Array of Strings and join it into one String? • Did you also know that you can split a String into an Array? String[] a = { "One", "string", "to", "rule", "them", "all…" }; String tolkien = join(a, " ");// tolkien == "One string to rule them all…" String b = "Another string to bind them…“ ; String[] tolkien2= split(b, " ");// tolkien2 == { "Another", "string", "to", "bind", "them…" } IAT 800

  16. Special characters • Split based on spaces (" ") • tab: "\t" • new line: "\n" • other escape characters include"\\" "\"" ( \ tells the computer to look to the next character to figure out what to do that's special.) String twolines = "I am on one line.\n I am \ton another."I am on one line.I am on another. IAT 800

  17. We started with Processing in… // any code here, no methods line(0,0,20,20); // methods! // global varsint a; // methodsvoid setup(){ } void draw(){ } // …with classes // (all of the above and then)class Emotion { //fields //constructor //methods } // …and subclasses! // (ALL of the above, and…) class Happy extends Emotion { //new fields //constructor //methods } IAT 800

  18. Processing is actually a Java Class // Java-Mode!!! class Uneasy extends PApplet { // void setup() and void draw() as normally … //methods //classes and subclasses } IAT 800

  19. Java Mode • Allows you to program in pure Java • Can import classes that aren’t normally imported into a Processing app • Importing means making a classes available to your program – the Java API docs tell you where classes are • In Java mode, create a class that extends PApplet • Normally, all Processing applets extend PApplet behind the scenes • setup(), draw(), etc. are methods of the class extending PApplet IAT 800

  20. A Java-mode program class MyProgram extends PApplet { void setup() { … } void draw() { … } void myTopLevelMethod() { … } class Text { // Text is just an example int xPos, yPos; String word; … } } Notice that any classes you define are inside the top class IAT 800

  21. Why use Java-mode? • Java-mode gives you access to the entire Java SDK • We need access to some SDK classes for HTML parsing that Processing doesn’t make visible by default • Java-mode helps you to understand how Processing is built on-top of Java • All those “magic” functions and variables are just methods and fields of PApplet that your program inherits IAT 800

  22. Libraries! • Libraries are other classes (in .java or .jar files ) • Use import nameoflibrary.nameofmethod; (e.g., import video.*; ) • Now with Java-mode, you can ALSO put your programs in multiple files • A file for each class • Create new tabs (files) with that button in the upper right IAT 800

  23. Recap • Strings • Methods and concatenation • Strings and Arrays IAT 800

More Related