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>
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.
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);.....
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;
}
}
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?
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.
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)
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.
Bookmarks