ok ,here is my doubt,may be its a very silly question but i want to know the explanation.
Which one of the following is a correct way to call methodA? Assume data is a non-empty reference to an array object, and lbl is a non-empty reference to a JLabel object. There are 4 options. 1)methodA(data[], lbl); 2)methodA( data[10], lbl); 3)methodA( data, lbl); 4)methodA( int [] data, lbl);
According to me the answer may be 2,but the answer is 3 can anyone explain me why the answer is 3 . Thanks in advance.
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Sorry i did not show the method its shown below.
public void methodA (int [] vals, JLabel display){ // do something .... }
the argument "data" itself would suffice when the actual argument is of array. It actually refers the base address (ie data[0]) of the array thereby in the called method you would get the reference.
in case of your Answer 2, its nothing but the 10th element of the array what you pass to invoke - which will NOT match the syntax of the method declaration.
That was a good point. But i did tell in a general way and context. In that case, the answer 3 seems to be right but not answer 2. Because the question was just having the plain options and not in detail.
In your case, answer 3 is wrong as it would be of sending the address of 2D array data[][]. But since the answer 3 was right, i think it might have been asked in the same assumption as that of mine.
Looking at the source code of the method definition what you gave,
The method definitely accepts only a *1D* array as an actual argument. In that case the answers may vary depends on how you invoke. There are two possibilities..
Case 1: If you have defined a 1D array
Case 2: If you have defined a 2D array,
But the question was very generically asking which one is right, and by default you can go for the 1D array and not the 2D array i guess. If then, you can even go for data[0][1][2][3] also right?