The original post was about an ArrayList which is different to an array. One aspect is that ArrayList can only hold objects. Because of that, if your code absolutely has to identify the type of object you can always use the instanceof keyword in an if statement. Or as Mike Conley and Mario Campaz pointed out, you can use features of polymorphism to do whatever is appropriate to each object instance automatically.
BTW, if you put a primitive type into an ArrayList it will be converted automatically to the equivalent wrapper class object so for example int becomes Integer, boolean becomes Boolean etc. You can also put a null reference into an ArrayList but that's not recommended because you can end up with null pointer exceptions if you try to do something with that element. On 01/01/2010, at 2:12 PM, Retnuh wrote: > Well there are ways of pulling two different objects out from an > Array, > but you will pull them all out and how are you sure of which one you > are > going to get? > > Stephen > > > > > > > MARIO CAMPAZ wrote: >> >> To answer this: >> >> Why did you put them in one array instead of seperating them into >> different ones. I thunk you should have seperated them at first and >> add them to the corrct array. >> >> If you put them in one array, then you can exploit the polymorphic >> OO- >> design capabilities. >> Leaving the runtime engine take care of the differences using dynamic >> binding. >> Please, correct if I am wrong! >> >> Mario >> >> >> On Thu, Dec 31, 2009 at 1:45 AM, Stephen Hunter <[email protected] >> <mailto:[email protected]>> wrote: >> >> Why did you put them in one array instead of seperating them into >> different ones. I thunk you should have seperated them at first >> and >> add them to the corrct array. >> >> Now how are you tellingthe difference between the coffee and >> juice? >> This will probably be the equation to check against in your if >> statement. >> >> Now for the "for" loop, there is an array called a "for each" >> array >> witch will itterate thru an array of objects. Just Google it and >> you >> will see the syntax, it is easy to use. >> >> >> Stephen >> >> >> >> >> >> >> >> Sent from my iPhone >> >> On Dec 30, 2009, at 6:36 PM, DrybLrac <[email protected] >> <mailto:[email protected]>> wrote: >> >>> hello all I have an arraylist problem...I created a program to store >>> coffee and juice objects in a arraylist bout how do I display them >>> with a for loop. Inside the for loop I know I need a if >> statement to >>> see if the current object in the for loop is coffee or juice. but >>> don't know how to write it...any suggestions? >>> >>> -- >>> To post to this group, send email to >> [email protected] >> <mailto:[email protected]> >>> To unsubscribe from this group, send email to >> [email protected] >> <mailto:javaprogrammingwithpassion%[email protected]> >>> For more options, visit this group at >> http://groups.google.com/group/javaprogrammingwithpassion?hl=en >> >> -- >> To post to this group, send email to >> [email protected] >> <mailto:[email protected]> >> To unsubscribe from this group, send email to >> [email protected] >> <mailto:javaprogrammingwithpassion%[email protected]> >> For more options, visit this group at >> http://groups.google.com/group/javaprogrammingwithpassion?hl=en >> >> > > -- > To post to this group, send email to > [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/javaprogrammingwithpassion?hl=en -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
