Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
RaviKran R Korllipara
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am following Kathy Sierra's book for JCP and in the first chapter there is question on arrays.

public class CommandArgsThree{
public static void main(String args[]){
String [][] argCopy = new String[2][2];
int x;
argCopy[0] =args;
x = argCopy[0].length;
for(int y=0;y<x;y++)
{
System.out.println(" " + argCopy[0][y]);
}
}
}

and the command line invocation,

java CommandArgsThree 1 2 3

Answer is it will display 1 2 3

My argument is how can we assign an array with 3 elements to a reference variable which was referring to an array with two elements.

Thanks in advance.
 
RaviKran R Korllipara
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please give me a reason
 
Abhijit Sontakey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In a two dimensional arrays, the second dimension doesn't matter. So the no. of rows in arrays need to be fixed. However the length of each of these rows can be any number. Hence the first row can have 3 elements and seconf 2 elements. The following code also works for two dimensional arrays:

public class CommandArgsThree {

public static void main(String[] args){
String [][] argCopy = new String[2][2];
int x;

argCopy[1] = new String[] {"1","2","3"};
argCopy[0] = new String[] {"1","2","3","4","5"};

x = argCopy[0].length;

for(int y=0;y<x;y++){
System.out.println(" " + argCopy[0][y]);
}
}
}

Let me know if this helps.
 
Gokul Somasundaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then tell me why is this not working?
public class Test{
public static void main(String args[]){
String [][] argCopy = new String[2][2];
int x;
x = args.length;
for(int y=0;y<x;y++)
{
argCopy[0][y] = args[y];
System.out.println(" " + argCopy[0][y]);
}
}
}

The above two programs worked because the reference has been repointed. If you try to access the object instead of the reference, then you will get the error.

Please correct me, if i am wrong.

Thanks,
Gokul.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The above two programs worked because the reference has been repointed.


Yes you are right..and the new dimension can accommodate all the values.



But in your program ...the original object has not been modified of its dimensions ..and you are trying to assign value outside its dimension of 2..so you are bound to get a runtime exception..

ArrayIndexOutOfBoundsException...


If you try to access the object instead of the reference, then you will get the error.


Didnt get that...??


[ October 24, 2005: Message edited by: A Kumar ]
 
manoj kushwaha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String [][] argCopy = new String[2][2];

I agrre with you Gokul. In above declaration argCopy is an array object that contains array of String. Now argCopy[i] contains reference of some array of String. Here according to declaration it is containing references to two arrays of length 2.

now
int x;
x = args.length;
for(int y=0;y<x;y++)
{
argCopy[0][y] = args[y];
System.out.println(" " + argCopy[0][y]);
}


Here the program will work if length of array args is 2 or less than 2, but, if length of array args is greater than 2 then ArrayIndexOutOfBoundsException exception will occur.

If there is any mistake please correct me.
 
Gokul Somasundaram
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar,
I think, i may be a bit unclear. Actually i meant the same thing, which you have said. I am saying that piece of code won't work and throw an error.

Manoj,
Yep, we are on the same boat.

Thanks,
Gokul.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think, i may be a bit unclear. Actually i meant the same thing, which you have said. I am saying that piece of code won't work and throw an error.



Yes you are right that it will throw an error..

I mentioned the reason for that..



But in your program ...the original object has not been modified of its dimensions ..and you are trying to assign value outside its dimension of 2..so you are bound to get a runtime exception..

ArrayIndexOutOfBoundsException...


Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic