Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Values of Hash Map to be 2-dimensional array

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
show us the code.
 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shanky Sohar wrote:show us the code.




 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should work fine.....

 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation Andrew... so the System.out.println doesn't resolve the statement by itself...
 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Andrew Monkhouse..
 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys for the information
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Andrew Monkhouse
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Praveen Srinivasan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is poorly stated and the answers reflect it.

A better question might be:  How can you use a hash map to track changes to a sparsely populated multidimensional array?

Almost all multidimensional arrays are sparsely populated as once you get past 3 dimensions, people just can't conceptualize a fully populated array (IMHO)

First, you need a hash key that describes the array.  An easy way to do this is separate it by a special character.  Computer systems do this for disk access where a path is actually the map into a sparsely populated array.  So you could use "/","\" or ":" depending on your preference.  I tend to use ":" because, well, Macintosh and I go way back.  So, the key into your hash map looks like this:  "dim1:dim2:...:dimN".  With this, you can either place the entire array into the hash map or you can use the hashMap to create meta information on your array like date, time or other info.

So then your hash map is simply:

Key: "dim1:dim2:...:dimN" and your value is the value.
 
Marshal
Posts: 70285
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DU: welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic