Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2-d arrays

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic