Daniel
Daniel

Reputation: 153

store 2 different objects in array

How do i store 2 different objects in array? So when i have for example object Worker ("name","age",Payroll) then another object that is Computer ("price","specification","..."). And I store these objects in a class.

class Record
{
  top = -1;
  private Worker array_w[];
  private Computer array_c[];

But then i have to do this:

 public void insert(Worker number) {
    if ((top + 1) < array_w.length)
     { top += 1; array_w[top] = number; }

 public void insert(Computer number) {
    if ((top + 1) < array_c.length)
     { top += 1; array_c[top] = number; }
 }

to insert each object into the new class.

How do I make it so there is only one array that stores any object.

Upvotes: 2

Views: 2323

Answers (5)

Jan
Jan

Reputation: 13858

You could use an array of Object or a shared base class

 private Object [] data;

but then you'd have instanceof Computer and instanceof Worker all over the place. And think of "how many Computers per Worker are there?" - simple division vs. loop-and-count.

My advice: keep separate members. Maybe List <Computer> instead of array even.

Upvotes: 1

StackFlowed
StackFlowed

Reputation: 6816

This is not a good design but if you wanted to then what you could do is have

First create an interface :

public interface MarkerInterface {
    public String print();
}

Then use that to implement both classes

public class Worker implements MarkerInterface {
   ...
   @Override 
   public String print() {
       return "worker class what ever you want to print"
   }
}

public class Computer implements MarkerInterface {
   ...
   @Override 
   public String print() {
       return "computer class what ever you want to print"
   }
}

Now create an array in Record class

public class Record
{
    MarkerInterface[] objects = new MarkerInterface[10];
    ...
}

Now you can instantiated the array of type MarkerInterface and store both of them.

Remember when you want to use the object from array you need to do

if(obj isinstanceof Computer) {...} else if(obj isinstanceof Worker) {...} 

or if the method is defined in the interface then you can simply do

System.out.println(obj.print());

Upvotes: 3

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

In addition to all of the other excellent (in some cases) suggestions you could always create a mashup class that can hold either of two types.

class OneOf<A, B> {

    A a;
    B b;

    public A getA() {
        return a;
    }

    public OneOf setA(A a) {
        this.a = a;
        this.b = null;
        return this;
    }

    public B getB() {
        return b;
    }

    public OneOf setB(B b) {
        this.b = b;
        this.a = null;
        return this;
    }

    @Override
    public String toString() {
        return a != null ? Objects.toString(a) : Objects.toString(b);
    }

}

Upvotes: 1

Propagandian
Propagandian

Reputation: 452

I would advise against mixing Objects, but if you must have one array, there are a few things you could do:

Have both objects inherit the same Object, or Implement the same interface:

public class Worker extends MyMainClass
public class Computer extends MyMainClass

OR

public class Worker implements IMyInterface
public class Computer implements IMyInterface

You then have:

MyMainClass[] myArray;

OR

IMyInterface[] myArray;

(Preferably the interface)

Alternatively, if the Worker and Computer go hand in hand, write a Wrapper and insert both in the same index.

public class Wrapper {
    private Worker worker;
    private Computer computer;
    ...
}

public void insert(Wrapper wrapper) {
    ...
}

And then you have an array:

Wrapper[] myArray;

Upvotes: 6

SMA
SMA

Reputation: 37023

I am assuming you have a common interface or class above Computer and Worker class. If that's the case, you could do something like:

 MyTopInterface[] array = ..

Now you could just use same array and populate either worker or computer objects in it.

Upvotes: 2

Related Questions