initializing arrays

edited February 2014 in Programming Questions

sorry to keep on going on about declaring and initializing statements but in the below example why do we initialize the array at the top (global) and then initialize it again in setup is the the top part Mover[] movers = new Mover[20]; just another way of declaring an array?and if so why would we declare it with the new function

    Mover[] movers = new Mover[20];

    void setup() {
      size(800,200);
      for (int i = 0; i < movers.length; i++) {
        movers[i] = new Mover(); 
      }
    }

    void draw() {

      background(255);

      for (int i = 0; i < movers.length; i++) {
        movers[i].update();
        movers[i].display(); 
      }
    }

Answers

  • edited February 2014
    1. Instantiate the array and assign its reference to a variable: Mover[] movers = new Mover[20];
    2. Instantiate all objects and assign their references to each slot of the array thru' the variable: movers[i] = new Mover();
  • edited February 2014 Answer ✓

    More detailed attempt explanations:

    • Java automatically initializes fields w/ a default value according to its data-type.
    • All non-primitive fields start w/ null. So when we declare Mover[] movers, movers contains value null.
    • Value null is an invalid object reference. So we gotta instantiate a compatible type object and assign it to the variable.
    • After that, we'd have movers pointing to a valid array object rather than null.
    • An array object is instantiated w/ all of its slots w/ default initial values in the same manner as for field data-types.
    • Since that is an array of Mover objects, all of its slots would start w/ null values.
    • So we'd still need to initialize each of its slots, not the array itself, w/ compatible Mover data-type references.
    • Usually a regular for loop, w/ its iterator representing current array's index, will have the job done.
    • There's also a special array instantiation w/ {}, in which we choose the initial values for each of its slots:

    Mover[] movers = { new Mover(), new Mover(), new Mover(), new Mover(), new Mover() };

  • ok so by reference you mean movers? could we say that in the class section we have built an object of type array and given it a specific array value and in the setup or constructor section we are building each element in the array and giving each element a set of object,primitive or null, values.

    it still appears to me as though we still constructing 2 objects when only one will do is there a good reason to do this other than to declare the no of instantiations you want to construct ie Mover[20] or is it just a subjective way the guys who built java use to deal with arrays

    Cheers

  • sorry wrote that just before i saw your more updated explanation reading it now

  • edited February 2014

    It still appears to me as though we're still constructing 2 objects when only one will do.

    You're mixing up the array data-structure instantiation & its slot instantiations.
    If you need 20 Mover objects, you're gonna need 21 instantiations: The array itself, plus its 20 Mover slots! :P

  • ok i see so we are declaring and initializing the array itself at the class level as one object and instantiating our 20 other objects that make up the array in setup.

  • edited February 2014

    That's right! 1st the structure, then its elements. :D
    Only a little extra curiosity: Arrays in Java aren't a class! @-)
    Java arrays can only be instantiated and accessed through []. Along the special initialization case w/ {}. b-(

  • i see :) so is this why we have to declare one object as type array at the class level and then access all elements in the array through that object :-/

  • You can think of the first declaration as making an egg box: it has slots to put eggs, but it is empty. Then, in setup(), you fill the box with eggs...
    As said, it is not "initialized twice", these are different operations. One without the other is prone to fail.

Sign In or Register to comment.