Why can't I append a new vector to an array of vectors?

Hi Sages,

I'm new to Processing and trying to reverse understand from Python.

I have vTr, an array of 2D vectors, with a single element (the starting coordinates of my object) in it. When I try to append vN (new coordinates) to vTr, I get "Type mismatch, "java.lang.Object" does not match with "processing.core.PVector[]":

xPos = xPos + speed * vP.x;

yPos = yPos + speed * vP.y;

vN = new PVector(xPos, yPos);

vTr = append(vTr, vN); //This underlines in red and delivers the message above.

Does this mean I have to use an ArrayList? The Processing 3.0 page claims append() works with objects.

Thanks for any help!

Answers

  • edited July 2016

    Read the reference on append

    use casting: like (PVector) or (PVector[]) or so

    [edited]

  • Thanks for your swift feedback Chrisir!

    I had read the reference on append() and noted the example:

    SomeClass[] items = (SomeClass[]) append(originalArray, element)

    This didn't make any sense to me but following your advice I changed line 7 to:

    vTr = (PVector) append(vTr, vN);

    but now it says "processing.core.PVector" does not match with "processing.core.PVector[]" which I guess is getting closer to working.

    What am I doing wrong? I've declared vTr as follows and got no errors:

    vS = new PVector(startX, startY);

    vTr = new PVector[1];

    vTr[0] = vS;

    Thanks again for your attention!

  • Whoa! vTr = (PVector[]) NOT (PVector)! Checking now! Thanks!!!

  • Hooray! That did it. Thanks much Chrisir. Can you repost the same answer so that I can respond accurately to the value of your help?

  • If my pull requested were accepted years ago, there'd be no need for any (casts) like (PVector[]): :-<

    https://GitHub.com/processing/processing/pull/2903

  • edited July 2016 Answer ✓

    Do you mean like this?

    vTr = (PVector[]) NOT (PVector)!

  • I'd recommend using an ArrayList rather than append(). An ArrayList is designed to be modifiable, an array isn't and append seems like a kludge.

    But, if you know how many elements you need and it's constant then use an array, initialise it to the correct size and then fill in the items.

  • edited July 2016

    An ArrayList is designed to be modifiable, an array isn't; and append() seems like a kludge..

    That's mostly true. However if those append() happen more occasionally, like in mousePressed() and such, I don't see any problems about sticking on regulars arrays.

    And as I've alluded before, I had a very nice rejected pull request about 2 years ago which would eliminate the need of (cast) for all types when dealing w/ arrays. =((

    Below's a demo about append(), which depends on expand() btW, w/ a PVector[] array.
    But that pull request had solutions for all the other array functions too: :ar!

    // forum.Processing.org/two/discussion/17524/
    // why-can-t-i-append-a-new-vector-to-an-array-of-vectors#Item_8
    
    // GoToLoop (2016-Jul-14)
    
    import java.util.Arrays;
    
    PVector[] vecs = {
      new PVector(400, 350), 
      new PVector(.5, -3.8, 1e3), 
      PVector.random3D(this).mult(100)
    };
    
    void setup() {
      // Displays current array[] of PVector:
      printArray(vecs);
      println();
    
      // No need for (PVector[]) cast for my own append()'s implementation:
      vecs = append(vecs, new PVector(TAU, EPSILON, RAD_TO_DEG));
    
      // Now the new resized array[] got 1 extra PVector:
      printArray(vecs);
      exit();
    }
    
    static <T> T[] append(T[] arr, T value) {
      arr = expand(arr, arr.length + 1);
      arr[arr.length - 1] = value;
      return arr;
    }
    
    static <T> T[] expand(T[] arr, int newSize) {
      return Arrays.copyOf(arr, newSize);
    }
    
  • Thanks koogs and GoToLoop for your responses!

    I have to admit that my first inclination as someone with more experience in Python was to complain (loudly) that there should be no need for casting if one has set up one's data types correctly (Bravo GoToLoop!) and no need for an ArrayList if an Array can do everything an array (or "list") is supposed to do. (ArrayList strikes me as the most redundant term possible.)

    Why are there methods like append() if you are not supposed to use them with Arrays? Yes, Python has immutable tuples and mutable lists, but the nomenclature reminds you that tuples are immutable lists and there are not a bunch of methods that let you do with tuples that which you should not do.

    I am trying to begin simply with Processing and thought I could start my business (creating Missile Command) with an Array. Now I understand that ArrayLists should be considered mutable versions of Processing's Arrays which should be employed as if they are immutable. Tuple is a goofy name but it does not seem to me to be as inherently misleading and redundant (respectively) as Array and ArrayList.

  • Why are there methods like append() if you are not supposed to use them with Arrays?

    Those array length-changing methods exist for the purpose of occasional amendments.

    Since in Java all arrays are of fixed-length, it's very CPU intense to create another array of diff. length, arrayCopy() everything from old to new array, then assign the new values, all the time. #:-S

    So we need to minimize that slow process as much as we can.
    That's why ArrayList, and other List containers, was introduced! :-bd

    An ArrayList got 1 regular array internally. However it splits its size() from its length capacity.

    It starts off w/ capacity = 10 and size() = 0.
    When we add() anything to it, it 1st checks out whether capacity has been reached.
    If not, it append() the value and increase its size().

    Otherwise, it clones its internal array w/ double of its current capacity.
    If it is 10 capacity, it becomes 20 now. Then it append() the add() element. *-:)

    As you can notice, it minimizes the # of times its internal array changes.
    While Processing's own append() creates a new array and arrayCopy() everything all the time! b-(

  • Answer ✓

    In short, the decision to pick a regular array or some other List container depends whether its length is gonna stay the same forever, or at least very occasionally it changes, or it changes a lot. :p

  • edited July 2016

    Now I understand that ArrayLists should be considered mutable versions of Processing's Arrays...

    Both regular arrays and List containers are mutable!
    Diff. is the former are of fixed-size while the latter of dynamic-size. L-)
    Python tuples are both immutable and of fixed-size btW. 8-|

  • Thanks! I think I have it now: Arrays are mutable in terms of item values but immutable in terms of the number of items.

Sign In or Register to comment.