JavaRanch » Java Forums »
Java »
Java in General
| Author |
Values of Hash Map to be 2-dimensional array
|
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
|
How to put a 2-dimensional array into the value field of a HashMap.. is there any way to do it...please reply with an example prog
|
Preparing for SCJP6.0
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
A "two dimensional array" is just an array of arrays. And an array is just an object.... so, you do it the same way you would for any other object.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Henry Wong wrote:
A "two dimensional array" is just an array of arrays. And an array is just an object.... so, you do it the same way you would for any other object.
I tried the same way as any other Object but i can't add the values to the HashMap...can you provide with a example...
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Praveen Srinivasan wrote:
I tried the same way as any other Object but i can't add the values to the HashMap...can you provide with a example...
Can you show us what you tried?
Henry
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Henry Wong wrote:
Can you show us what you tried?
I tried a simple code as below i don't know if this is right way
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
I assume that you want to put the array into the hashmap, and not the element of the array, right? To do that....
Henry
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Henry Wong wrote:
I assume that you want to put the array into the hashmap, and not the element of the array, right? To do that....
Henry
I tried that too but it shows a runtime error as below
Exception in thread "main" java.lang.ClassCastException: [[Ljava.lang.Object;at hash.main(hash.java:40)
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10816
|
|
In that case, there must be something that you are not showing us. What you have provided does work:
Perhaps you should consider posting an very simple block of code that demonstrates your problem?
|
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
I did the coding as below
I'm using JDK1.3 could that be a problem?.... Also there was no complie time error only runtime error...
But without the print statement(line no: 9) no errors are shown...
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:
Henry Wong wrote:
Can you show us what you tried?
I tried a simple code as below i don't know if this is right way
even this will work but it will put only the value of arr[0][0] at the specified,,not a complete array
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10816
|
|
Praveen Srinivasan wrote:I'm using JDK1.3 could that be a problem?.... Also there was no complie time error only runtime error...
But without the print statement(line no: 9) no errors are shown...
What was the runtime error?
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:
even this will work but it will put only the value of arr[0][0] at the specified,,not a complete array
Yeah... but is there possibilities to put the complete array
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:I did the coding as below
I'm using JDK1.3 could that be a problem?.... Also there was no complie time error only runtime error...
But without the print statement(line no: 9) no errors are shown...
what i think is,
for JDK1.3,you have to do the explicit casting
on this line
System.out.println(map.get("1"));
as
System.out.println((String)map.get("1"));
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:
Shanky Sohar wrote:
even this will work but it will put only the value of arr[0][0] at the specified,,not a complete array
Yeah... but is there possibilities to put the complete array
I think Henry already explains that see above
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Andrew Monkhouse wrote:
Praveen Srinivasan wrote:I'm using JDK1.3 could that be a problem?.... Also there was no complie time error only runtime error...
But without the print statement(line no: 9) no errors are shown...
What was the runtime error?
The error is
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:
I think Henry already explains that see above
That doesn't work its shows a runtime error as in my previous post
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:
Shanky Sohar wrote:
I think Henry already explains that see above
That doesn't work its shows a runtime error as in my previous post
see my earlier post for why you are getting a error
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:
see my earlier post for why you are getting a error
I did that casting too but i got the same runtime error
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
|
show us the code.
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:show us the code.
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10816
|
|
Shanky is partially correct, insofar as you need to do some casting for what you are currently doing. However the bigger concern is that you need to consider what you are putting into your map and what you are getting out.
Looking at the code again (with a few lines of my own thrown in):
At line 8 you are putting an array of arrays into the Map.
At line 9 you are trying to retrieve the array of arrays and print it in one step. Early versions of Java did not know know what to do here - they expected to be provided with a String. And so you get the error telling you that an array of arrays is not a string.
Later versions of Java didn't do much better: while they no longer give you a runtime exception, they still don't know how to handle an array of arrays. So if you were to try this using a later version of Java you might get an output of something like:
So I can see that it is an array of arrays of Object. But that is all I can meaningfully get from this.
As you can see in my line 11, as far as the Map is concerned you are only storing some object in the Map. You know that this object is an array of arrays, however the compiler does not know or care: you can put any object into that map.
This is where Shanky's comment comes into play: In line 12 I am retrieving the object from the map, and simultaneously telling Java that the object I am retrieving is an array of arrays. It is not enough to assign it to a variable that is an array of arrays: I must explicitly cast it to that array of arrays.
This is still not good enough to print out the contents - the System.out.println method still does not know what to do with an array of arrays. One solution is to use the Arrays.toString method to display the contents of an array. However it only works on an individual array. So my loop from lines 14 to 16 goes through every outer array, printing the contents of the inner array. With a result like:
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:
Shanky Sohar wrote:show us the code.
it is because we have put a array into a HashMap which is a object but while retriving we are try to cast it to a string.
but object is a superclass and string is subclass.
Note:We cannot cast subclass to a superclass.
That why you are geting error
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
This should work fine.....
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
|
Thanks for the explanation Andrew... so the System.out.println doesn't resolve the statement by itself...
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:This should work fine.....
Sorry shanky that didn't solve the problem.... it showed the runtime error as Andrew gave...
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:Thanks for the explanation Andrew... so the System.out.println doesn't resolve the statement by itself...
No..never..
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
@Andrew Monkhouse..
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
|
Thanks a lot guys for the information
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Praveen Srinivasan wrote:
Sorry shanky that didn't solve the problem.... it showed the runtime error as Andrew gave...
It is because of the difference between JDK1.3 and JDK1.6..
by the way..
Welcome to JavaRanch
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Shanky Sohar wrote:
It is because of the difference between JDK1.3 and JDK1.6..
I think that's not the problem as Andrew said
Andrew Monkhouse wrote:
Later versions of Java didn't do much better: while they no longer give you a runtime exception, they still don't know how to handle an array of arrays.
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
Andrew Monkhouse wrote:
This is still not good enough to print out the contents - the System.out.println method still does not know what to do with an array of arrays. One solution is to use the Arrays.toString method to display the contents of an array. However it only works on an individual array. So my loop from lines 14 to 16 goes through every outer array, printing the contents of the inner array.
java.util.Arrays.deepToString(arr) can be used in that case. It iterates in through the array elements & calls toString() in them. If the elements are in turn arrays again, it goes in deeper..and so on.. This comes in handy when you want to print multi-dimensional arrays.
|
OCPJP 6
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10816
|
|
Vinoth Kumar Kannan wrote:
Andrew Monkhouse wrote:
This is still not good enough to print out the contents - the System.out.println method still does not know what to do with an array of arrays. One solution is to use the Arrays.toString method to display the contents of an array. However it only works on an individual array. So my loop from lines 14 to 16 goes through every outer array, printing the contents of the inner array.
java.util.Arrays.deepToString(arr) can be used in that case. It iterates in through the array elements & calls toString() in them. If the elements are in turn arrays again, it goes in deeper..and so on.. This comes in handy when you want to print multi-dimensional arrays.
Unfortunately java.util.Arrays.deepToString(arr) has only been around since JDK 1.5, and Praveen is using JDK 1.3. Otherwise an excellent suggestion.
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Andrew Monkhouse wrote:
Vinoth Kumar Kannan wrote:
Andrew Monkhouse wrote:
This is still not good enough to print out the contents - the System.out.println method still does not know what to do with an array of arrays. One solution is to use the Arrays.toString method to display the contents of an array. However it only works on an individual array. So my loop from lines 14 to 16 goes through every outer array, printing the contents of the inner array.
java.util.Arrays.deepToString(arr) can be used in that case. It iterates in through the array elements & calls toString() in them. If the elements are in turn arrays again, it goes in deeper..and so on.. This comes in handy when you want to print multi-dimensional arrays.
Unfortunately java.util.Arrays.deepToString(arr) has only been around since JDK 1.5, and Praveen is using JDK 1.3. Otherwise an excellent suggestion.
this is very great.i got output like this
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4163
|
|
Praveen Srinivasan wrote:I'm using JDK1.3 could that be a problem?
1.3? I'd guess that was EOLed before you wrote your first line of Java code.
And your sigline says
Preparing for SCJP6.0
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Praveen Srinivasan
Greenhorn
Joined: Aug 28, 2010
Posts: 17
|
|
Darryl Burke wrote:
 1.3? I'd guess that was EOLed before you wrote your first line of Java code.
And your sigline says
Preparing for SCJP6.0
Will change it soon...
|
 |
 |
|
|
subject: Values of Hash Map to be 2-dimensional array
|
|
|
|