Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList Question 1

Status
Not open for further replies.

k4ghg

Technical User
Dec 25, 2001
191
US
I am using an ArrayLists to store values from JTextFields. Some of the values are strings, others integers and still others doubles. Currently I am retriveing items using:
aName.get(count).toSting().trim()

Is there away to retrive to value as an integer or a double?

Thanks...Ronnie
 
Hi,

I hope this is what you are looking for

// for Integer
Integer countInt = (Integer)aName.get(countInt);
// for Double
Double countDou = (Double)aName.get(countDou);

Cheers
Venu
 
That if you stored them as Integers or Doubles ...

Cheers,

Dian
 
Hi Dian - Would you please explain what you mean by ...That if you stored them as Integers or Doubles ...I thought an Arraylist is stored as an object and can contain differnt types. Thanks...
 
Hi,

I think Dian was referring , If you trying to type cast one Object to another Object in the above scenario then it will throw java.lang.ClassCastException

Ex:
Code:
ArrayList list = new ArrayList();
list.add(new String("100"));
list.add(new Integer("100"));
list.add(new Double("100"));

Integer one = (Integer)list.get(0);
Integer two = (Integer)list.get(1);
Double three = (Double)list.get(2);

// one will throw java.lang.ClassCastException two and three will not

Cheers
Venu
 
So you could do:-
Code:
Object oItem = list.get(iPosition);
String sItem = null;
Integer iItem = null;
Double dItem = null;
if ( oItem instanceof String ){
   sItem = (String)oItem;
   //process the String
} else if ( oItem instanceof Integer ){
   iItem = (Integer)oItem; 
   //process the Integer
} else if ( oItem instanceof Double ){
   dItem = (Double)oItem;
   //process the Double
}
to process typed instances of an element at iPosition. An alternative would be to have a separate ArrayList for each expected type.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
You could do that, and that would work, but would be an Object Oriented sin.

You hava a wide collection of classes and objects, icluding Interger, Doubles, Strings a nd some zillions mores, you should carefully select which one of the is suitable to not only hold but access and maintain each type of data.

If you store them as raw Objects, accessing them with instanceof (clause that should be erased from Java), you're losing all that funtionality.

You have some textfields, right? Then you will get Strings for them, you'll have to check if they have numeric format and convert it to Integers or Doubles in some way so ... why storing them in a single ArrayList without type?

Just store them as Strings and convert later or create a couple of ArrayLists, one for each type.

Cheers,

Dian
 
Dian is absolutely correct ... my example above should include a 'health warning' - "This code will get your code working, but it breaks 'OO' best practices. You should redesign your solution to make proper use of polymorphism."

However
Dian said:
.. instanceof (clause that should be erased from Java) ..
there may be cases when use of instanceof is acceptable ...
... should I duck now?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Well, I was trying to joke, but I guess my english doesn't always follow me.

Of course instanceof is useful and in some cases it offers a clearer and faster solution but everytime I use it dark clouds come to my mind: maybe this small instanceof is just a workaround to hide some design error I hava at higher level?

Cheers,

Dian
 
Your English is pretty good Dian. What is your 'Mother Tongue' by the way?

Your warning of the 'sin' of instanceof was, in my opinion, well made. All too often it comes to mind as a solution to some problem; I also see the 'dark clouds'.



Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks...I do appreciate your kind suggestions. I'm certain the snipet works. My problem is that I'm still using Java 1.4 and it does not support some of that code.
 
Opps...I responded to the wrong post. Sorry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top