1

I have objects set up like this, data coming from a service:

DataType1_1 has array [DataType2_1, DataType2_2, DataType2_3]
DataType1_2 has array [DataType2_2, DataType2_3]

I want them to be re-arranged so I have objects like this:

DataType2_1 has array [DataType1_1]
DataType2_2 has array [DataType1_1, DataType1_2]
DataType2_3 has array [DataType1_1, DataType1_2]

Hope that makes sense. How could I do this?

EDIT:

Have Animal objects (dog, cat, mouse) which each have an array of possible fur colors. I need to switch the order, so I have fur colors that each have an array of possible animals that correspond.

Dog has array [Brown, White, Black]
Cat has array [Brown, White, Red]
Mouse has array [White, Black]

Need this:

Brown has array [Dog, Cat]
White has array [Dog, Cat, Mouse]
Red has array [Cat]
Black has array [Dog, Mouse]
5
  • Can you give real example? replace datatype1_1 with something else and post your code. BTW, this is not related to Android. Commented Jun 5, 2015 at 16:29
  • When you say DataType, you mean object, right? Commented Jun 5, 2015 at 16:52
  • Is it possible for you to have the colors as objects, and not of strings? If so, you can play around with inheritance to simplify this a little. Commented Jun 5, 2015 at 20:23
  • Also, you might want to give a more descriptive title. It presently sounds like you want to move elements around in an array. Commented Jun 5, 2015 at 20:24
  • Yeah the colors are objects as well actually. I was just trying to keep this generic. Commented Jun 5, 2015 at 20:34

2 Answers 2

1

What you are doing is called "array transposing" and yes, it is not Java specific.

But here is an example:

java multi-dimensional array transposing

Basically, loop through Array[x][y] to change to Array[y][x]. You just have to think of it as a 2 dimensional array instead of 2 different 1-dimensional arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think that's what I want to do.. not sure, I added more details. I know it was confusing with the ambiguous dataTypes.
Yes - if you look at it as an array of animals as "rows" and colors as "columns" you are trying to change it to animals as "columns" and colors as "rows" (and some of them have "0" or "null" values)
0

This might be a push in the right direction. I'm using just Strings, instead of custom class objects, to demonstrate the rearranging of arrays using Streams from Java 8.

public static void main(String[] args) throws Exception {
    String[] Dog = {"Brown", "White", "Black"};
    String[] Cat = {"Brown", "White", "Red"};

    // Join the arrays together to get distinct values
    String[] join = new String[Dog.length + Cat.length];
    System.arraycopy(Dog, 0, join, 0, Dog.length);
    System.arraycopy(Cat, 0, join, Dog.length, Cat.length);
    Object[] distincts = Arrays.stream(join).distinct().toArray();

    // Rearrange data into a Map
    Map<String, List<String>> rearranged = new HashMap();
    for (Object obj : distincts) {
        rearranged.put(obj.toString(), new ArrayList());
        if (Arrays.stream(Dog).anyMatch(dt -> dt.contentEquals(obj.toString()))) {
            rearranged.get(obj.toString()).add("Dog");
        }
        if (Arrays.stream(Cat).anyMatch(dt -> dt.contentEquals(obj.toString()))) {
            rearranged.get(obj.toString()).add("Cat");
        }
    }

    // Print rearranged results
    rearranged.keySet().stream().forEach((key) -> {
        System.out.println(key + " has array " + rearranged.get(key));
    });
}

Results:

Red has array [Cat]
Brown has array [Dog, Cat]
White has array [Dog, Cat]
Black has array [Dog]

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.