Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused

 
saied ims
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. public class CommandArgsThree {
2. public static void main(String [] args) {
3. String [][] argCopy = new String[2][2];
4. int x;
5. argCopy[0] = args;
6. x = argCopy[0].length;
7. for (int y = 0; y < x; y++) {
8. System.out.print(" " + argCopy[0][y]);
9. }
10. }
11. }
the answer is :
D. In line 5, the reference variable argCopy[0], which was referring to an array with
two elements, is reassigned to an array (args) with three elements
how did assign array of 2 elements to array of 3
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

argCopy[0] = args;
how did assign array of 2 elements to array of 3


args is one dimensional array
argcopy is two dimensional array

argcopy[0] can have address of single dimensional array or null
argcopy[0][0] can have values

argCopy[0] = args;
In our case we are having address of args to argcopy
[i.e address of one dimensional array to argcopy[0]]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Referring to the first post: What is the full question?
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Actual Question:
how did assign array of 2 elements to array of 3


Q should have been: how did assign array of 3 elements to array of 2?

Q could be: Size of one dimensional array(args) is greater than size created for the first row of two dimensional array(argCopy[0]). How does it work?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic