Go Back   nV News Forums > Software Forums > Software Development

Newegg Daily Deals

Reply
 
Thread Tools
Old 10-29-07, 12:51 PM   #1
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default Casting Help

Java:

I have an Interface called Analyzer and 3 classes that implement the interface. In the processing class I want to loop an Arraylist of the objects and call the methods they implement from the Interface... I know I can do this with the newest version of java:

private Arraylist<Analyzer> list = new Arraylist<Analyzer>;

SummaryReport summary = new SummaryReport();
UniqueToken token = new UniqueToken();
TokenCount count = new TokenCount();

list.add(summary);
list.add(token);
list.add(count);

for (Analyzer item : list) {
processToken();
}

but how do I do that same thing for older versions of java?? help anyone??
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 10-29-07, 10:25 PM   #2
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default Re: Casting Help

Quote:
Originally Posted by jcrox
Java:

I have an Interface called Analyzer and 3 classes that implement the interface. In the processing class I want to loop an Arraylist of the objects and call the methods they implement from the Interface... I know I can do this with the newest version of java:

private Arraylist<Analyzer> list = new Arraylist<Analyzer>;

SummaryReport summary = new SummaryReport();
UniqueToken token = new UniqueToken();
TokenCount count = new TokenCount();

list.add(summary);
list.add(token);
list.add(count);

for (Analyzer item : list) {
processToken();
}

but how do I do that same thing for older versions of java?? help anyone??
you can't use generics for 1.4 and earlier, so for creating your arraylist:

private Arraylist list = new Arraylist();

You also can't use the funky new for loop parameters... easiest thing is probably an iterator/while loop:

Iterator iterator = item.iterator();
while(iterator.hasNext()) {
Analyzer temp = (Analyzer)iterator.next(); //this explicitly casts the "Object" coming out of your ArrayList into an "Analyzer"

//do stuff with your analyzer here....
}

Generics (the ArrayList<Analyzer> kind of notation) does basically the same thing. The only difference is, it'll catch logic errors at compile time that would otherwise be caught at run time.... in other words, the compiler will find an error if you tried to stick an object other than an "Analyzer" into the arraylist if you're using generics.... if you tried that with normal casting, the compiler will be perfectly happy, but when you try the explicit cast yourself (the Analyzer temp = (Analyzer)iterator.next() thing), you'll get an "Invalid class cast exception" if the object in your list is anything but an Analyzer.... (or compatible derived class).
rhink is offline   Reply With Quote
Old 10-30-07, 03:19 AM   #3
bugmeplz
*BANNED*
 
bugmeplz's Avatar
 
Join Date: Sep 2006
Posts: 427
Default Re: Casting Help

Why are you using < 1.5? You should really consider upgrading to at least 1.5, which added many useful constructs to the language.
bugmeplz is offline   Reply With Quote
Old 10-30-07, 10:40 PM   #4
jcrox
Mahna Mahna
 
jcrox's Avatar
 
Join Date: Jul 2006
Location: Madison, Wi
Posts: 6,134
Default Re: Casting Help

Quote:
Originally Posted by bugmeplz
Why are you using < 1.5? You should really consider upgrading to at least 1.5, which added many useful constructs to the language.
Im currently using jdk1.6.0_03 but my instructor is insisting that we learn how to do stuff in 1.4 as apparently there are several companies in the area that still use the old stuff.

Thanks for the help!!
__________________

Gaming

MSI MPower Z87 | 4770k | CNPS9900MAX-B | 8GB G.Skill 1866 | EVGA GTX760 |ThermalTake 850w | X-Fi Titanium | HAF | Asus PA248Q | Lycosa | Death Adder | Win8 Pro 64

Dev/Backup

MSI GD65 | 2500k | H50 | 8GB G.skill | GTX570 | Ubuntu 13.10

Laptop

17" Inspiron | i5 | 4GB DDR3 | 500GB HDD | Win7 Pro 64

jcrox is offline   Reply With Quote
Old 10-30-07, 11:30 PM   #5
rhink
Registered User
 
Join Date: Jul 2005
Posts: 434
Default Re: Casting Help

'tis true, and if you do end up working with Java in the real world, you never know when you might be called upon to update or maintain older code, as the majority of java code out there is going to be using older language constructs.... so you at least need to be able to understand how to read it. I'm forced to use 1.4 for some stuff due to products that don't officially support 1.5... and believe it or not, trying to run them with a 1.5 virtual machine does seem to cause issues.
rhink is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:35 PM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.