Another newb question on arrays flexibility?

edited September 2015 in Questions about Code

this example is from the the orig processing book it uses custom function to transfer array data I seem not to understand the application of this one: Page 311 Data 4: Arrays

float[] data = { 19.0, 40.0, 75.0, 76.0, 90.0 };
float[] halfData;
void setup() {
halfData = halve(data); // Run the halve() function
println(data[0] + ", " + halfData[0]); // Prints "19.0, 9.5"
println(data[1] + ", " + halfData[1]); // Prints "40.0, 20.0"
println(data[2] + ", " + halfData[2]); // Prints "75.0, 37.5"
println(data[3] + ", " + halfData[3]); // Prints "76.0, 38.0"
println(data[4] + ", " + halfData[4]); // Prints "90.0, 45.0"
}

float[] halve(float[] d) {
float[] numbers = new float[d.length]; // Create a new array
arraycopy(d, numbers);
for (int i = 0; i < numbers.length; i++) { // For each element,
numbers[i] = numbers[i] / 2; // divide the value by 2
}
return numbers; // Return the new array
} 

lame shortcut

                float[] data = { 19.0, 40.0, 75.0, 76.0, 90.0 };
                void setup(){
                  for(int i = 0 ; i <data.length ; i++){
                  float  x  = data[i] ;
                  float y  = data[i]/2 ; 
                   println(x,y);
                  }
                }

both gives same result ? what is the disadvantage of the shorter code ( lame "shortcut" ) would the orig code be flexible in any application ?

is array the most important aspect in creating good visual designs? should i really be mastering it if I want to focus on making crazy designs (automated boom boom style?)

Answers

  • The first example shows how to get use of a function that returns an array, the second only prints the result of division with no way to use this data in future.

    The array is essential for programming, as it helps to store similar data in one container, read "From several variables to arrays" .

    Array is a general programming concept that has no direct relation to visual design. Usually, almost every program uses arrays or different containers, as it is hard to deal with separate variables (ie choose name, iterate over them). You'll understand a use of it, as you'll create more complex applications.

  • edited September 2015

    The "longer" example is cloning the original array.
    We do that when we don't wanna lose the original array.

    The "lame" example is simply traversing the array and doing some calcs and displaying the results.
    The array itself isn't touched and keep its original content.

    There's another approach when we really wanna modify the original array.
    In this case, we don't care to keep the original content around.

    As you can see, the "longer" approach is very verbose.
    It relies on arrayCopy() to transfer the original content to the new cloned array.
    But there's a shorter & faster way to do it via clone() method instead:

    static final float[] halveTheArray(float... original) {
      final float[] half = original.clone();
      for (int i = 0; i != half.length; half[i++] /= 2.0); 
      return half;
    }
    
Sign In or Register to comment.