1

How to set values for two dimension array of objects in java.

following is my for loop :

Object[][] hexgenSecurityInferenceData = null;
for (String methodName: knowGoodMap.keySet()) {
    hexgenSecurityInferenceData = new Object[][] {
        {
            (KnownGoodInfoRO) knowGoodMap.get(methodName), new Object[] {
                (MethodPropertiesRO) methodPropertiesMap.get(methodName), (List) methodParametersMap.get(methodName)
            }
        },
    };
}

this prints only one row of data. I am sure that i make mistake when adding values to Array of Object but really don't know how to fix.

Kindly help me to fix this

1
  • you are instantiating your Object[][] array inside the loop. Commented May 15, 2013 at 5:54

3 Answers 3

4

You can't add elements to an array - you can only set elements in an array.

I suggest you have a List<Object[]> instead:

List<Object[]> hexgenSecurityInferenceData = new ArrayList<Object[]>();
for (String methodName:knowGoodMap.keySet()) {
    hexgenSecurityInferenceData.add(new Object[] {
        knowGoodMap.get(methodName),
        new Object[] {
            methodPropertiesMap.get(methodName),
            methodParametersMap.get(methodName)
        }
     });
 }

(I've removed the casts as they were pointless... you're storing the values in an Object[] anyway. The only benefit of the casts would be to cause an exception if the objects were of an unexpected type.)

You could still use an array if you really wanted, but you'd need to create it with the right size to start with, and then keep the "current index". It's then generally harder to use arrays than lists anyway.

If you really need an array, you can create one from the list:

Object[][] array = hexgenSecurityInferenceData.toArray(new Object[0][]);

Doing it in two stages this way will be simpler than directly populating an array up-front.

I'd actually suggest two further changes:

  • Don't just use Object[] for this... create a type to encapsulate this data. With your current approach, you've even got a nested Object[] within the Object[]... any code reading this data will be horrible.
  • Use entrySet() instead of keySet(), then you don't need to fetch the value by key
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Jon, but i have to go ahead with the approach i have started with, and it is a TestNG which has to return Object[][], the present approach works but i am not able to set elements to it, as Stepehan pointed out i instantiate Object inside, i have now followed this hexgenSecurityInferenceData[0][0] = new Object(); but after this how to add elements to it ??
@Anto: You simply can't. You're going to have to accept that arrays are fixed size... you'd have to create it with the right size to start with. However, you can convert a List<Object[]> into an Object[][]... I'll edit my answer with that code.
@Anto: Also note that if we'd known more context to start with, I wouldn't have ended up suggesting the change to avoid Object[][].
Jon Skeet, You always rock, Thanks for the answer +1.
1

You have a matrix of objects Object[][] so if you want to populate this 2-d array you have to do something like:

Object[][] hexgenSecurityInferenceData=new Object[10][10];
for(int i=0; i<10;i++){
   for(int j=0; j<10;j++){
        hexgenSecurityInferenceData[i][j] = new Object();
   }
}

But as well pointed by Jon its better to have your own implementation/encapsulation instead of using Object

4 Comments

after this how to add elements to hexgenSecurityInferenceData??
since its a 2-d array there are 2 indexes (one for each dimension), so you have to iterate through each combination to populate the array
Thanks Stephan, how to add this data {(KnownGoodInfoRO)knowGoodMap.get(methodName), new Object[] {(MethodPropertiesRO)methodPropertiesMap.get(methodName),(List)methodParametersMap.get(methodName) }}, };
Its more tricky using 2-d array since you have to specify the size at instance creation , the method use by Jon Skeet is dynamic and it should work for you.
1

Using List is the best way to resolve this. However still you can do using object[] by initializing array.

Object[][] hexgenSecurityInferenceData = new Object[knowGoodMap.keySet().size()][];
int i = 0;
for (String methodName : knowGoodMap.keySet()) 
{
  hexgenSecurityInferenceData[i][0] = new Object[][]
  {
     {(KnownGoodInfoRO) knowGoodMap.get(methodName), 
         new Object[]{(MethodPropertiesRO) methodPropertiesMap.get(methodName), (List) methodParametersMap.get(methodName)}
     }
  };
   i++;
 }

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.