File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ArrayList of Different Datatypes!! 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 "ArrayList of Different Datatypes!!" Watch "ArrayList of Different Datatypes!!" New topic
Author

ArrayList of Different Datatypes!!

Vikas
Ranch Hand

Joined: Dec 16, 2004
Posts: 63
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

Joined: Nov 30, 2004
Posts: 134
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....


Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Vikas
Ranch Hand

Joined: Dec 16, 2004
Posts: 63
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

Joined: Feb 28, 2005
Posts: 986
To avoid ClassCastException,you can do:


Namma Suvarna Karnataka
Vikas
Ranch Hand

Joined: Dec 16, 2004
Posts: 63
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

Joined: Nov 30, 2004
Posts: 134
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

Joined: Feb 27, 2002
Posts: 8919

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?


Groovy
Vikas
Ranch Hand

Joined: Dec 16, 2004
Posts: 63
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList of Different Datatypes!!