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);
}