1

I am supposed to convert hashes into arrays, but it throws a java.lang.ArrayStoreException at me. I'm taking it one step at a time, trying to see how I can structure it, but it won't run.

Hashes:

{
    objectOne : {
        attributeOne : 1,
        attributeTwo : 2
    },
    objectTwo : {
        attributeOne : 3,
        attributeTwo : 4
    }
}

into Arrays with structure:

[
    {
        name : 'objectOne',
        attributes : {
            attributeOne : 1,
            attributeTwo : 2
        }
    }, {
        name : 'objectTwo',
        attributes : {
            attributeOne : 3,
            attributeTwo : 4
        }
    }
]

My code throws an:

java.lang.ArrayStoreException: java.lang.String
    at java.util.AbstractCollection.toArray(AbstractCollection.java:171)
    at hashToArray2.main(hashToArray2.java:36)

I've tried to change the parameters to:

Object[] keys = map.keySet().toArray(new Object[map.size()][]));
Object[] values = map.values().toArray(new Object[map.size()][]));

But it yields the same problem.

Code:

public class hashToArray2{
    public static void main (String[] args) throws ClassNotFoundException{
        Class.forName("hashToArray2");
        System.out.println("hashToArray class successfully loaded");

        //Creating object1 + input values
        HashMap<String, Integer> obj1 = new HashMap<String, Integer>();
        obj1.put("attributeOne", 1);
        obj1.put("attributeTwo", 2);

        //Creating object2 + input values
        HashMap<String, Integer> obj2 = new HashMap<String, Integer>();
        obj2.put("attributeOne", 3);
        obj2.put("attributeTwo", 4);

        //Combining obj1+2
        //HashMap<String, Integer> map = new HashMap<String, Integer>();
        //map.putAll(obj1);
        //map.putAll(obj2);

        //to array
        Object[][] arr1 = new String[obj1.size()][2];
        Object[][] arr2 = new String[obj2.size()][2];
        //obj1
        Object[] keys1 = obj1.keySet().toArray(new Object[obj1.size()][]);
        Object[] values1 = obj1.values().toArray(new Object[obj1.size()][]);

        for(int i = 0; i < arr1.length; i++){
            arr1[i][0] = keys1[i];
            arr1[i][1] = values1[i];
        }

        for(int j = 0; j < arr1.length; j++){
            for(int k = 0; k < arr1[j].length; k++){
                System.out.println(arr1[j][k]);
            }
        }

        //obj2
        Object[] keys2 = obj2.keySet().toArray(new Object[obj2.size()][]);
        Object[] values2 = obj2.values().toArray(new Object[obj2.size()][]);

        for(int a = 0; a < arr2.length; a++){
            arr2[a][0] = keys2[a];
            arr2[a][1] = values2[a];
        }

        for(int b = 0; b < arr2.length; b++){
            for(int c = 0; c < arr2[b].length; c++){
                System.out.println(arr2[b][c]);
            }
        }
    }
}

2 Answers 2

1

The exception is caused by this line of code:

Object[] keys1 = obj1.keySet().toArray(new Object[obj1.size()][]);

So, what is it doing: it calls toArray(T[]) on a Set<String>. toArray() takes an array as argument, and stores all the values of the set (String instances, in this case), in that array. But you're passing new Object[obj1.size()][] as argument, i.e. an array of Object[], i.e. an array of arrays of objects. A String is not an Object[], hence the error.

I don't really know what you're trying to do, given that your description really doesn't match with the code. What I know is that your code is begging for custom classes with attributes, instead of maps and arrays.

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

Comments

0

I don't like to just post code, but I think this is what you want.

List<Map> list = new ArrayList<>();
for(Map.Entry<String, Integer> entry : hashes.entrySet()) {
    Map newMap = new HashMap();
    newMap.put("name", entry.getKey());
    newMap.put("attributes", entry.getValue());

    list.add(newMap);
}

1 Comment

Thanks for that! I actually just re-wrote the whole thing, but your code helped. :)

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.