Question

I have a method, aModify(), that takes two object references, a1 and a2, and two String values, dim1, and dim2.

public static Object aModify(Object a1, String dim1, Object a2, String dim2);

a1 and a2 will be passed in as arrays, and dim1 and dim2 will be passed in as array dimensions (i.e dim1 = "0", dim2 = "0 0").

The goal behind this method is to take the array a2 at dim2 and copy the value(s) found there to a1 at dim1.

I have successfully copied the two arrays into new instances to manipulate, but I'm really confused on how to translate the String values dim1 and dim2 into array dimensions, and then use those dimensions to copy values.

So far, this is what I have for the method.

Object copy1 = Array.newInstance(a1.getClass().getComponentType(), Array.getLength(a1));
Object copy2 = Array.newInstance(a2.getClass().getComponentType(), Array.getLength(a2));

copy1 = MyUtility.aClone(a1);
copy2 = MyUtility.aClone(a2);

aClone() is a method I've written that returns a copy of whatever array that's passed into the method.

Arrays a1 and a2 also have different dimensions. The array a1 is a three-dimensional array where a2 is a four-dimensional array.

Was it helpful?

Solution

I am not sure why your method does not take the arguments as int directly, but if you are just trying to convert the String "0" to an int, you can just use Integer.parseInt().

If you want to convert the string "0 0" into two separate integers, you can use String.split() and then Integer.parseInt() on each element in the resulting array.

Based on your description of your goal, you probably want something like this?

int t_dim1 = Integer.parseInt(dim1);
String[] twodims = dim1.split(" ");
int t_dim2_1 = Integer.parseInt(twodims[0]);
int t_dim2_2 = Integer.parseInt(twodims[1]);

Update: It appears that the OP wants to handle arbitrary numbers of dimensions. For this, we need to be a little more clever. First, we write a helper function.

int[] parseAndConvert(String dims) {
    String[] dimArray = dims.split(" ");
    int[] retVal = new int[dimArray.length];
    for (int i = 0; i < retVal.length; i++) 
       retVal[i] = Integer.parseInt(dimArray[i]);
    return retVal;
}

Then, inside aModify, we will need to do something like this to find the element and set it. Note this is untested, but it should get you pretty close.

public static Object aModify(Object a1, String dim1, Object a2, String dim2) {
    int[] dims1 = parseAndConvert(dim1);
    int[] dims2 = parseAndConvert(dim2);
    // Find the element to copy.
    Object copyMe = a1;
    for (int i = 0; i < dims1.length; i++) 
         copyMe = java.lang.reflect.Array.get(copyMe, dims1[i]);

    // Find the nested array holding the object we want to overwrite
    Object target = a2;
    for (int i = 0; i < dims2.length - 1; i++) 
         target = java.lang.reflect.Array.get(target, dims2[i]);

    Array.set(target, dims2[dims2.length - 1], copyMe);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top