Week5 Workshop

 

arrayTutorial2

Array Explained

1) From processing.org

An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first element in the array is [0], the second element is [1], and so on. Arrays are similar to objects, so they must be created with the keyword new.

Each array has a variable length, which is an integer value for the total number of elements in the array. Note that since index numbering begins at zero (not 1), the last value in an array with a length of 5 should be referenced as array[4] (that is, the length minus 1), not array[5], which would trigger an error.

2) Array Example

int[] numbers = new int[3];
numbers[0] = 90;  // Assign value to first element in the array
numbers[1] = 150; // Assign value to second element in the array
numbers[2] = 30;  // Assign value to third element in the array
int a = numbers[0] + numbers[1]; // Sets variable 'a' to 240
int b = numbers[1] + numbers[2]; // Sets variable 'b' to 180

Array Hands-on

1) Picture array example link

2) Button array example link 1 and link 2

 


APPENDIX_ArrayList

An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is resized dynamically. This can be very convenient, but it’s slower than making an array of objects when using many elements. Note that for resizable lists of integers, floats, and Strings, you can use the Processing classes IntList, FloatList, and StringList.

An ArrayList is a resizable-array implementation of the Java List interface. It has many methods used to control and search its contents. For example, the length of the ArrayList is returned by its size() method, which is an integer value for the total number of elements in the list. An element is added to an ArrayList with the add() method and is deleted with the remove() method. The get()method returns the element at the specified position in the list. (processing.org)

ArrayList is an implementation of List , backed by an array. All optional operations including adding, removing, and replacing elements are supported. This class is a good choice as your default List implementation. (Android Developers)

1) Particle example using Array (Daniel Shiffman)

int total = 10;
Particle[] parray = new Particle[total];
 
void setup() {
This is what we’re used to, accessing elements on the array via an index and brackets—[ ].
  for (int i = 0; i < parray.length; i++) {
    parray[i] = new Particle();
  }
}
 
void draw() {
  for (int i = 0; i < parray.length; i++) {
    Particle p = parray[i];
    p.run();
  }
}

2) Particle example using ArrayList (Daniel Shiffman)

int total = 10;
Have you ever seen this syntax before? This is a new feature in Java 1.6 (called “generics”) that Processing now supports. It allows us to specify in advance what type of object we intend to put in the ArrayList.
ArrayList<Particle> plist = new ArrayList<Particle>();
void setup() {
  for (int i = 0; i < total; i++) {
An object is added to an ArrayList with add().
    plist.add(new Particle());
  }
}
 
void draw() {
The size of the ArrayList is returned by size().
  for (int i = 0; i < plist.size(); i++) {
An object is accessed from the ArrayList with get(). Because we are using generics, we do not need to specify a type when we pull objects out of the ArrayList.
    Particle p = plist.get(i);
    p.run();
  }
}

Recourses

ArrayList & particle example

http://natureofcode.com/book/chapter-4-particle-systems/
http://www.learningprocessing.com/examples/chapter-23/example-23-2/

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s