File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays " Watch "Arrays " New topic
Author

Arrays

RaviKran R Korllipara
Greenhorn

Joined: Sep 02, 2005
Posts: 2
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

Joined: Sep 02, 2005
Posts: 2
Can anyone please give me a reason
Abhijit Sontakey
Ranch Hand

Joined: Sep 26, 2005
Posts: 67
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

Joined: Jul 17, 2005
Posts: 20
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

Joined: Jul 04, 2004
Posts: 979
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

Joined: Jul 12, 2005
Posts: 6
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

Joined: Jul 17, 2005
Posts: 20
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

Joined: Jul 04, 2004
Posts: 979
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays