简体   繁体   中英

Create a 2D array in java to store two kinds of objects

I have a superclass Agent . The superclass Agent has two subclasses: 1. User ; 2. Seller . And these two sub classes are the superclass of User_action and Seller_action . I want to create two objects. The first object is of class User_action . The aim behind creating this object is that it should inherit the variables and methods of class Agent and class User (and, I hope I am achieving this aim.). And, I create the second object from class Seller_action with the similar objective in mind (inherit variables and methods from class Agent and class Seller .).

Once, I create these objects (or instances I believe) I want to store them in a 2D array. I don't know how to do this. And, this is my first attempt with java, so I have read very basic stuff and now I am trying to improve as I code.

Now, my problem is:How can I store two kinds of obejcts..in something. You see, the reason I want to do this is because I want all the objects to be present on a geographical coordinate and I want to know every one's coordinate or geographical position (x,y). And, as I come from MATLAB the best way to do this is to store it in an Matrix (2D array in JAVA).

I will appreciate any advice. Many thanks!

I think what you should do is have 2 super classes: 1 - Agent, 2 - Action. The each has 2 sub classes and the action has a User value. You can then create an array of Actions

I believe, you are looking to design the classes in the following manner such that objects ua and sa(defined below) inherit all the values from their parent:

Agent is the Parent class:

class Agent {  //the parent class

}

Class User is sub class of Agent

class User extends Agent{   //user is subclass of Agent
    int Agent_1,Agent_2;
    User()
    {
        Agent_1 = 1;
        Agent_2 = 2;
    }
}

Class Seller is sub class of Agent

class Seller extends Agent{   //Seller is sub class of Agent

    int Seller_1, Seller_2;
    Seller()
    {
        Seller_1 = 3;
        Seller_2 = 4;
    }
}

User_action is subclass of User

class User_action  extends User{   //user action is subclass of User
    int UA_1, UA_2;
    User_action()
    {
        UA_1=5; UA_2 = 6;
    }

}

Seller_action is subclass of Seller

class Seller_action extends Seller{   //Seller action is subclass of Seller
    int SA_1, SA_2;
    Seller_action()
    {
        SA_1=7; SA_2 = 8;
    }
}

And then you create the following Objects(which inherit all the values from their parent):

User_action ua = new User_action();
Seller_action sa = new Seller_action();

ArrayList<Object>[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList<Object>(); // add another ArrayList object to [0,0]
table[0][0].add(ua); // add object to that ArrayList

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM