Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList of Different Datatypes!!

 
Vikas
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have an ArrayList of two datatypes like String, float, while trying to retrive the actual values of these, I do the following

Iterator iter = myArrayList.iterator();
while(iter.hasNext()) {
String s = (String)iter.next();
}

And obviously, this throws the java.lang.ClassCastException. Now my question is as to how to retrieve the actual values of my Strings and floats from this ArrayList??

Thank you

Regards
Vikas
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas,

Originally posted by Vikas Sharda:

String s = (String)iter.next();


This statement will throw a classcast exception when it encounters a float data type. In order to extract them safely, you need to apply a float cast like
float f = (float)iter.next();

However I do not know if there is any particular order that you have used to store strings & floats...if there is then you can use a for loop with some conditional...though that would be another question.

Hope I helped....
 
Vikas
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah.... that has been a great help.... but how would I get my float/string value in this case if I know the exact location??

[ April 13, 2005: Message edited by: Vikas Sharda ]
[ April 13, 2005: Message edited by: Vikas Sharda ]
 
Arjunkumar Shastry
Ranch Hand
Posts: 986
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid ClassCastException,you can do:
 
Vikas
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the follwoing:

while(iter.hasNext()) {
try{
Object obj=iter.next();
if(obj instanceof String)
{
String cont=obj.toString();
}
if(obj instanceof Float)
{
System.out.println("cont");
}

it does nothing

Thank you
Vikas
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know the exact location of the strings & floats (suppose they appear alternately), you can use a for loop instead of the while loop. In the for loop, you can have an if-condition based on the index of the for loop.

for(int index = 0; index<aList.size(); index++)
{
if(index/2 == 0) //even places in list
{
//cast for string
}

else
{
//cast for float
}
//move pointer to next element
} // end of for

Hope this helps..
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vikas Sharda:
I tried the follwoing:

while(iter.hasNext()) {
try{
Object obj=iter.next();
if(obj instanceof String)
{
String cont=obj.toString();
}
if(obj instanceof Float)
{
System.out.println("cont");
}

it does nothing

Thank you
Vikas


The above code should work as long as there are appropriate objects in the list. How are you adding into the list?
 
Vikas
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding Value Objects of one int, then four strings and then a float in the end. OH.... I suddenly found that I am adding VO in this ArrayList.... Now I have solved this.... I am sorry to bother you all and thanks for your help.

Regards
Vikas
[ April 13, 2005: Message edited by: Vikas Sharda ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic