0

I'm facing this task:

I have class A and class B. These two classes are different but almost the same. I need to somehow merge them into 1 Single array of objects so I will be able to use them later in a list that combines both classes.

Class A:

public class Followers {
    private String request_id;
    private String number_sender;
    private String state;

    public String getRequest_id() {
        return request_id;
    }

    public String getNumber_sender() {
        return number_sender;
    }

    public String getState() {
        return state;
    }
}

Class B:

public class Following {
    private String name;
    private String state;
    private String request_id;

    public String getRequest_id() {
        return request_id;
    }

    public String getName() {
        return name;
    }

    public String getState() {
        return state;
    }
}

I've tried doing this next move:

Object[] obj1 = (Object[]) followers;
Object[] obj2 = (Object[]) followings;

 Object[] completeArray = ArrayUtils.addAll(obj1, obj2);

Where followers and followings are both arrays of the corresponding classes. Then in my list adapter I use:

  if (values[currentItem] instanceof Followers) { BLA BLA BLA}

  else if (values[currentItem] instanceof Following) { BLA BLA BLA}

But I get this exception:

Caused by: java.lang.ArrayStoreException: source[0] of type json.objects.Following cannot be stored in destination array of type json.objects.Followers[]

What will be the best way to merge two arrays of different objects into one array?

Will just implementing the same interface between them do the job and then they will basically be in an array of the interface type?

what other ways do you recommend?

2
  • 2
    "Will just implementing the same interface between them do the job and then they will basically be in an array of the interface type?" That is a perfectly reasonable way to approach this if you essentially do the same thing with each type of object. Commented Aug 28, 2014 at 14:49
  • is there any connection between e.g. followers[0] and following[0] I mean would something like completeArray[0] = [followers[0], following[0]] do? If so, do also followers[] and following[] have the same length? Commented Aug 28, 2014 at 14:49

4 Answers 4

4

Try this

Object[] completeArray = new Object[0];
completeArray = ArrayUtils.addAll(completeArray, obj1);
completeArray = ArrayUtils.addAll(completeArray, obj2);
Sign up to request clarification or add additional context in comments.

Comments

3

If you make both classes implement a common interface you can manipulate arrays/lists of them as if they contains instances of the interface.

public interface Follow {

    public String getRequest_id();

    public String getState();
}

public class Follower implements Follow {

    private String request_id;
    private String number_sender;
    private String state;

    public String getRequest_id() {
        return request_id;
    }

    public String getNumber_sender() {
        return number_sender;
    }

    public String getState() {
        return state;
    }
}

public class Following implements Follow {

    private String name;
    private String state;
    private String request_id;

    public String getRequest_id() {
        return request_id;
    }

    public String getName() {
        return name;
    }

    public String getState() {
        return state;
    }
}

public void test() {
    List<Follow> all = new ArrayList<>();

    all.add(new Following());
    all.add(new Follower());

    for ( Follow f : all ) {
        String id = f.getRequest_id();
        String state = f.getState();
    }

}

Alternatively you could put them in a hierarchy:

public class Entity {
    private String request_id;
    private String state;

    public String getRequest_id() {
        return request_id;
    }

    public String getState() {
        return state;
    }
}

public class Follower extends Entity {

    private String number_sender;

    public String getNumber_sender() {
        return number_sender;
    }

}

public class Following extends Entity {

    private String name;

    public String getName() {
        return name;
    }

}

public void test() {
    List<Entity> all = new ArrayList<>();

    all.add(new Following());
    all.add(new Follower());

    for ( Entity f : all ) {
        String id = f.getRequest_id();
        String state = f.getState();
    }

}

Or you could make the extra fields into attributes.

enum Attribute {

    Follows,
    Followed;
}

public static class Entity {

    private String request_id;
    private String state;
    EnumMap<Attribute, String> attributes = new EnumMap<>(Attribute.class);

    public String getRequest_id() {
        return request_id;
    }

    public String getState() {
        return state;
    }

    // Factory to make entities.
    static Entity make(Attribute attribute, String value) {
        Entity e = new Entity();
        e.attributes.put(attribute, value);
        return e;
    }
}

public void test() {
    List<Entity> all = new ArrayList<>();

    all.add(Entity.make(Attribute.Follows, "Fred"));
    all.add(Entity.make(Attribute.Followed, "Gill"));

    for (Entity f : all) {
        String id = f.getRequest_id();
        String state = f.getState();
    }

}

There are an infinite number of possibilities.

Comments

0

USE concat

var combined= obj1.concat(obj2); // Merges both arrays

1 Comment

This is not Java. Groovy?
0

Try this.

  private Object[] appendObj(Object[] obj, Object newObj) {

    ArrayList<Object> temp = new ArrayList<Object>(Arrays.asList(obj));
    temp.add(newObj);
    return temp.toArray();

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.