Results 1 to 12 of 12
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default How do I retrieve an array from a different class?

    I'm using the Swing GUI with NetBeans 6.8. I have several classes, including my GameGUI class that I made using the Swing GUI designer.

    I have an array that is in a different class that I want to read data from. How do I read an array from outside the GameGUI class into the GameGUI class?

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by Psyclone View Post
    I'm using the Swing GUI with NetBeans 6.8. I have several classes, including my GameGUI class that I made using the Swing GUI designer.

    I have an array that is in a different class that I want to read data from. How do I read an array from outside the GameGUI class into the GameGUI class?
    The same way you read any other piece of data. Make sure that the scope (public, protected or default - private won't work in this case) is correct.

    In order to better help you, is your variable static or non-static? Do you access it directly (e.g. MyClass.myArray) or do you have accessors (e.g. myClass.getMyArray())? Hint: if you don't use an accessor method, you should.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    You can ask that other class for it.

    Java Code:
    GameGUI method...
    int[] data1 = differentClassObject.giveMeData();
    int[] data2 = differentClassObject.getMeCopyOfData();
    
    Different class...
    int[] data;
    public int[] giveMeData() { return data; }
    public int[] giveMeCopyOfData() { return data.clone(); }

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Thanks for the help.

    I still couldn't get it to work, so I just returned the arrays back to main.java and then passed them into the next class as a parameter.

    Or maybe that is the way it's supposed to be done and I just wasn't understanding. :(

  5. #5
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    I just returned the arrays back to main.java and then passed them into the next class as a parameter.
    This is almost never the right way to do things in Java.

    In Java -- or any Obect-Oriented Programming (OOP) language -- data resides in objects.
    Each class defines a kind of object. Objects are passed as parameter.
    Then the recipient gets the data from the object by calling a method defined by that object's class.

    Please re-read Turtle's response.

    To rephrase his response, suppose we have three classes, D, R, and M.
    M is the main class. D is the donor; it creates the arrays.
    R is the recipient that wants to use the arrays.

    M looks lilke this:
    Java Code:
        ...
        D datasource = new D(paameters for creating arrays);
        R recipient = new R(parameters to create recipient);
        recipient.processData(datasource);
        ...
    The D constructor (or some D methods) create the arrays and store them in D.
    The R class has a method "processData(D data)".
    Inside processData, R calls a method defined on D for getting an array from it:
    Java Code:
       ... // inside R
        DataType[] a1 = data.getArray1();
        OtherDataType[] a2 = data.getArray2();
        // process data from arrays a1 and a2
        ...
    So we see that D must have two methods
    Java Code:
       DataType[] getArray1() { ... }
       OtherDataType[] getArray2() { ... }

  6. #6
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    I think I FINALLY understand now. Let me try to verify what I am taking away from this though.

    Just to make sure I'm on the correct track, I'm just going to try to explain what I'm understanding the first part of code means.

    Java Code:
        ...
        D datasource = new D(paameters for creating arrays);
        R recipient = new R(parameters to create recipient);
        recipient.processData(datasource);
        ...
    datasource and recipient are the actual object names. If I want to create 2 objects (which are my array of objects) of Type D, then I would have to use the following code?

    Java Code:
        ...
        D datasource1 = new D(paameters for creating arrays);
        D datasource2 = new D(paameters for creating arrays);
        R recipient = new R(parameters to create recipient);
        recipient.processData(datasource1);
        recipient.processData(datasource2);
        ...

  7. #7
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    I think it's important to clarify a couple of things. Sorry if you already know them...

    When you create an array of anything, the array contains some defaults values.
    Examples:
    int[] iArray = new int[3]; // contains {0,0,0}
    boolean bArray = new boolean[3]; // contains {false,false,false}
    Object oArray = new Object[3]; // contains {null, null, null}

    You see that arrays of any type of Object will contain null. null is a reference to "no object". Arrays of objects are initially empty. You need to create objects and place them in the array yourself.
    oArray[0] = new Object();
    oArray[1] = new Object();
    oArray[2] = new Object();

    Zweibieren was talking giving information to a class that will be used to create the objects of the array that it contains. Sounds complicated but it's not.
    Java Code:
    class BoxHolder {
      BoxObject[] boxArray;
      BoxHolder(int numberOfBoxes, int sizeOfBoxes) {
        boxArray = new BoxObject[numberOfBoxes];
        for(int i=0; i<numberOfBoxes; i++) {
          boxArray[i] = new BoxObject(sizeOfBoxes);
        }
      }
      BoxArray[] getBoxes() { return boxArray; }
    }
    This code illustrates a class called BoxHolder. It's constructor is used to create an array of boxes. It then populates the array. And finally there is a method to get those boxes. You can get those boxes from outside of the class by calling the getBoxes method.

    Java Code:
    class Example {
      public static void main(String[] args) {
        int boxCount = 3;
        int boxSize = 10;
        BoxHolder bh = new BoxHolder(boxCount, boxSize);
        BoxObject[] boxArray = bh.getBoxes();
      }
    }
    This code illustrates creating that class and then accessing those boxes.

    You don't need to put arrays inside of other objects. The point is, you get at them by asking the object for them.

    Hope this helps :-)

  8. #8
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    First of all, I appreciate all the time, help and patience that everyone has given me. :) THANKS!

    Turtle, I think I am close to understanding it. I appreciate you simplifying it as much as possible, it helps a lot. Some of it I already knew and some of it is still fuzzy me, but either way it helps reinforce it.

    If you don't mind, I'd like to break down your code step-by-step and make sure I understand it fully.

    Java Code:
    class BoxHolder {
      BoxObject[] boxArray;
    This is the beginning of the class BoxHolder. It initializes an array named boxArray of type BoxObject.

    Java Code:
      BoxHolder(int numberOfBoxes, int sizeOfBoxes) {
        boxArray = new BoxObject[numberOfBoxes];
        for(int i=0; i<numberOfBoxes; i++) {
          boxArray[i] = new BoxObject(sizeOfBoxes);
        }
      }
    This is a method named BoxHolder with the input parameters numberOfBoxes and sizeOfBoxes. The FOR loop is the constructor which populates the array of length numberOfBoxes with initial values of 0 (false or null if they are of type boolean or String/Object).

    Java Code:
      BoxArray[] getBoxes() { return boxArray; }
    }
    This is a get method so that I can call the object from another class. It is also the end of this class.

    Java Code:
    class Example {
      public static void main(String[] args) {
        int boxCount = 3;
        int boxSize = 10;
    This is the beginning of the class Example which also contains the main method(?) which starts the program. The first thing the main method does is initialize the integers boxCount and boxSize and sets their values at 3 and 10, respectively.

    Java Code:
        BoxHolder bh = new BoxHolder(boxCount, boxSize);
    Here's part of the code that is still a little fuzzy to me. This is initializing the variable/object/class(?) bh of type BoxHolder with the parameters boxCount and boxSize? Does this just set aside a place in memory?

    Java Code:
        BoxObject[] boxArray = bh.getBoxes();
      }
    }
    This creates the array of objects named boxArray of type BoxObject by calling the getBoxes method in the class BoxHolder.

    Is all of that correct?

    I think I can immediately see part of my problem. I have been putting the constructors in the recipient classes instead of in the donor classes (as zweibieren referred to them in his example).

    I will redo my code with the constructors in the correct classes and see if that fixes my problem or if it is still deeper.

    Thanks for all the help so far!
    Last edited by Psyclone; 02-04-2010 at 11:05 PM. Reason: typo

  9. #9
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    OK, I'm confused again. :(

    What does...

    Java Code:
      BoxObject[] boxArray;
    ...do in your code? Is it trying to create an array of objects named boxArray of type BoxObject? I'm getting an error because of BoxObject[].

  10. #10
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    Java Code:
    class BoxHolder {
      BoxObject[] boxArray;
    This is just the definition of BoxHolder. It says that instances of the class BoxHolder will have an array of BoxObjects.
    When the constructor is called "new BoxHolder(numberOfBoxes, sizeOfBoxes)" then the memory is allocated for the array and the rest of the instance of the class.

    Java Code:
    BoxHolder(int numberOfBoxes, int sizeOfBoxes)
    Since this method has the same name has as the class (and no return type) it is a constructor.
    It is used to construct a new instance of this class. An instance of a class is called an object.


    Yes, the starting point of a java application is the "public static void main(String[] args)"


    Java Code:
        BoxObject[] boxArray = bh.getBoxes();
      }
    }
    This code asks the object bh to give it's BoxObject array.

    To break it down further...
    Java Code:
      BoxObject[] boxArray; // Declare a reference to an array of BoxObject array
      boxArray = bh.getBoxes(); // Ask bh to give the reference to it's BoxObjects array, and store the reference in boxArray
    
      // alternatively...
      boxArray = new BoxArray[5]; // Create an array that can store references to BoxArray objects
    So, you're doing alright. Very thorough.

    Just ask yourself if this data really belongs in the class you place it in.
    You'll get a feeling for it :-)

    Best of luck
    Last edited by Turtle; 02-04-2010 at 11:36 PM.

  11. #11
    Turtle is offline Member
    Join Date
    Nov 2007
    Location
    New Zealand
    Posts
    36
    Rep Power
    0

    Default

    It was a fictious object I made up for to illustrate storing a custom object in an array.

    You can make your own objects in Java. Just like this made up dog class...
    Java Code:
    class Example {
      public static void main(String[] args) {
        Dog dog = new Dog("Sam");
        dog.bark();
      }
    }
    class Dog {
      String name;
      Dog(String name) { this.name = name; }
      public void bark() { System.out.println(name + " says woof!"); }
    }
    Sorry to have confused you

  12. #12
    Join Date
    Dec 2008
    Location
    Manchester England
    Posts
    14
    Rep Power
    0

    Default

    A method such as

    Java Code:
    public type getArray(){ return myArray; }
    inside your outside class.

    Inside GUI class instance the outer class

    Java Code:
    OuterClass oc = new OuterClass();
    now you can retrieve the array with

    Java Code:
    type arr = oc.getArray();
    now you have a reference to your array.

Similar Threads

  1. How do I search an array from a different class?
    By Psyclone in forum New To Java
    Replies: 6
    Last Post: 02-04-2010, 01:21 AM
  2. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 08:24 AM
  3. Array project for class
    By circuspeanuts in forum New To Java
    Replies: 7
    Last Post: 04-17-2009, 01:44 AM
  4. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  5. Accessing array in other class
    By ce3c in forum New To Java
    Replies: 8
    Last Post: 02-23-2009, 12:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •