how to invoke an object using an array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: how to invoke an object using an array

  1. #1
    Join Date
    Oct 2004
    Posts
    34

    Question how to invoke an object using an array

    Is it possible to use an array to invoke an object?
    <pre>
    int[][] a1 = {
    { 1, 2, 3, },
    { 2, 2, 3, },
    { 2, 2, 3, },
    { 4, 4, 4, },

    };
    for(int i = 0; i <=3; i++){
    for(int j = 0; j < 3; j++)

    System.out.println(a1[i][j]);

    }
    so, instead of creating 4 objects
    obj myobj = new obj(1,2,3);
    obj myobj2 = new obj(2,2,3);.....
    ....
    I want to use one object and have the array invoke the object.
    </pre>

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    sorry matey, i dont understand, coudl you maybe re-phrase the question?
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    Are you asking if you can use a multidimensional array to store objects? If so, then yes you can. I'm not sure what you mean by "invoke an object" though.

  4. #4
    Join Date
    Oct 2004
    Posts
    34
    Not sure what the terms are? Instantiation...

    Ok, here goes...
    In the client I want to instantiate the objects with an array.
    so instead of creating four instances.
    obj myobj = new obj(1,2,3);
    obj myobj2 = new obj(2,2,3);.....

    I want the array...to instantiate one object.
    int[][] a1 = {
    { 1, 2, 3, },
    { 2, 2, 3, },
    { 2, 2, 3, },
    { 4, 4, 4, },
    };

    obj myobj = new obj(array here?[][]);

    so, the result would be to use one object, with the array feeding the object data.

    Hope that is clear...

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    Ok, now I understand.....well to do that you just need to create a constructor for the class that is capable of taking an array as a parameter.

    Code:
    public class SomeClass
    {
        int[][] anIntArray;
    
        public SomeClass(int[][] anIntArray)
        {
            //do anything you like with array here...
            //eg.....
            
            this.anIntArray = anIntArray;
        }
    }

  6. #6
    Join Date
    Oct 2004
    Posts
    34
    Thanks Mike, that helps a lot and makes things more clear. But, my problem is that the constructor is in another class(server class),

    public TriBoolean(int a, int b, int c ){
    this.side_a = a;
    this.side_b = b;
    this.side_c = c;
    }

    So, to sum up what I want...I have a server class that uses the constructor above. Then I have a client class in which I want to instantiate the objects using the array:

    public class theClient{
    TriBoolean myobj = new TriBoolean(a1[][]);.....
    .....
    So I'm not sure on how to set up the constructor as you stated in the server class, and then have the array process in the client class?

    Hope I'm still clear...lol

  7. #7
    Join Date
    Sep 2004
    Posts
    223
    not too sure about the coding behind this but couldnt you just use some kind of for looping and create the new object like this:

    MyObj a = new MyObj(array[0][0], array[0][1], array[0][2]);

    ???this would pass ints to the constructor, and if you looped it correctly then it would be ok...i think
    A kram a day keeps the doctor......guessing

  8. #8
    Join Date
    Feb 2004
    Posts
    541
    I still don't think i'm totally sure what you want to do lol. The array can't do any processing though. It is just a kind of box that holds values or references to objects. You can't make it do anything with the values though.

    You would have to write a method that takes all the values and makes the objects for you. Not knowing exactly what it is that the objects are or how they're used I'm not sure what the best way to do this would be though. The for loop suggestion would certainly work though. The method could store the created objects in an array, and then return the array of objects. This would work with any arbitrary number of objects.

    The method that does this could be a static method in the TriBoolean class. You pass it the array of values, it creates the objects of itself and passes them back in an array or some collection object.

  9. #9
    Join Date
    Sep 2004
    Posts
    223
    im assuming you are doing this so that you can save time on coding, but either way you are going to have to do alot of typing...
    if you do work out how to use an array, then your still going to have to create the array by hand, which could take a while, unless the array values are sequential or follow some pattern (ie not random)
    A kram a day keeps the doctor......guessing

  10. #10
    Join Date
    Oct 2004
    Posts
    34

    Thumbs up

    Thanks guys you both were a great help. Great info, the for loop works! That is what i needed.

    Mark

  11. #11
    Join Date
    Oct 2004
    Posts
    34

    Thumbs up The Results...

    I have posted the results of using the array(client class Describe.java). Thanks...

    The server class takes in three ints and tested in the boolean methods. if the property holds, then true else....
    then the method() values are passed to a toString() and the output is based on the return value of the methods().

    In the client I would have had to create an object for each triangle that I wanted to test for its property values.

    Just thought I post it.....for viewing
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center