aspose file tools*
The moose likes Java in General and the fly likes 2-d arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "2-d arrays" Watch "2-d arrays" New topic
Author

2-d arrays

Victoria Preston
Ranch Hand

Joined: Feb 03, 2006
Posts: 106
I need to make a two-d array but I nned it to contain floats and strings at the same time....how do I do this?
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
You can't. When you create an array, you have to declare its type, and then the array can only store elements of that type.

However, you can use more sophisticated containers than arrays for storing things. Here is an example using a container called an ArrayList to store doubles and Strings:


Using collection classes, like an ArrayList, is a little bit tricky. When you try to add an object like a String to an ArrayList, Java automatically converts the String to type Object before adding it. So really, just like with an array, you are limited to storing only one type, i.e. type Object. But, the Object class is the base class for all classes, including user created classes. Therefore, any object can be converted to type Object, and therefore only being able to store Object types in an ArrayList is really not a limitation--it's only a hindrance. The hindrance being that when you retrieve the elements of your ArrayList, you get them returned to you as type Object, so you have to convert them back to their orignal type.

If you add only objects of the same type to an ArrayList, for instance all String objects, then you can just cast every element to a String immediately after retrieving it, e.g.

String theFirstElement = (String) myArrayList.get(0);

On the other hand, if you add objects of various types to an ArrayList, for instance objects of type Double and objects of type String, then after you retrieve an element from the ArrayList, you don't necessarily know to what type you should cast the element. In that case, you can use the instanceof operator to determine what type is actually stored in the returned Object element in order to cast it to the right type.
[ October 06, 2006: Message edited by: sven studde ]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by sven studde:
You can't. When you create an array, you have to declare its type, and then the array can only store elements of that type.


You can't store floats and Strings in the same array, but you can store Floats and Strings in an array of Objects. (The capital F in Float means we are talking about the wrapper class java.lang.Float).

It would not usually be good design to do this, however. Perhaps you should take a step back and see if another data structure than 2D arrays would be more suitable. Perhaps tell us what you're really trying to achieve.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
jQuery in Action, 2nd edition
 
subject: 2-d arrays