• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning properties from / calling methods on a arraylist of objects  RSS feed

 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. First post so be gentle with me. I'll do my best.

I'm pretty new to all of this (not even sure of the right terms) but if I enforce that a List contains only a certain class (or subclass?) of object as follows:



And I know that object foo has a method bar.

I know I can run bar on all members of myList in a for loop but can I invoke the method on all objects in the list in a single call on myList? It strikes me that handling return values might be one reason this wouldn't work but if there are others then I think it would help my understanding to know what they are - sorry if it is a naive question.

If the answer is no, as I suspect but am unsure of, then I guess I should be learning about iterators?
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the short answer is, indeed, "no." Even if it weren't, you might find the results a bit odd, because you wouldn't know for sure the order in which your objects had their bar methods called. If bar changes any shared state (a static member, for example), the effects might be very hard to understand, and even inconsistent across multiple runs. (Potentially more exciting (read that, "worse") is Java's undying love of multitheading everything, which might even put you into situations where various bar methods are racing with each other).

One of the more experienced posters here might have a more elegant way to do it, but I think iterating across the list is a good start.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That won’t quite work, I am afraid, as you will realise if you try to compile it. The compiler will happily compile that, but give a warning about unchecked type. What you are doing is saying that you want a List of Foos, and then giving any old List. It is called using a raw type. You need to put generic information after the type of the implementation.Or, in Java7 only, you can write <> on the right instead of <Foo>, providing the type is simple enough that the compiler can work it out for itself.
Yes, you can call bar(), but not on all instances. You can call bar() on each instance (that’s complicated for a Saturday night, isn’t it), which means every instance in turn. Yes, you need an Iterator<Foo> (or maybe Iterator<>). I shall refer you to the Java Tutorials, only this section might not be what you want. Nor the for section and the while section. I had expected to find your solution there. I shall have to show you how they work; the recommended way is probably to use the for‑each loop shown last, but like the other versions, it uses an Iterator behind the scenes.As for return types, you didn’t supply enough details that I can give a sensible reply to that bit.

Advanced feature: look up the RandomAccess interface, and work out when it makes a traditional for loop efficient on a List.
Java is an imperative language; you can only call one method at a time. It might be possible to call multiple methods together in some languages, but not Java.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, thanks very much for the welcome and the answers. Turns out the original question wasn't the interesting bit after all!

I need to get my head around this concept of generics I think. I've put a test class together along the lines of:



This works fine and doesn't give any errors or warnings but this might be because I am using an IDE?

Then I tried switching in the commented code and I got a compilation error around the type of d as I'd hope.

So what advantage do I get from adding that second <Foo>declaration? I thought maybe because I initialized an array list with 10 entries that maybe I could sneak 10 non-foo items into those slots but when I try the following I still get a type error:


I'm not trying to be difficult but I want to understand what sort of issues I am leaving myself open to by using the first item below rather than the second.





As regards the iterator/for loop: thanks for that - I guess it was as I suspected - I'll check those reference and probably stick to the for each loop I coded originally. Disregard my comment on return types, that was more thinking out loud about evaluating a function on multiple objects simultaneously.

For reference I am using Java 7.


Thanks again
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:
I'm not trying to be difficult but I want to understand what sort of issues I am leaving myself open to by using the first item below rather than the second.






Simply, the second syntax is the correct syntax. The first syntax is allowed for backward compatibility purposes. Obviously, using the first syntax as written in your example is silly -- as why would you want to have the right hand side of an assignment use the old syntax? It is more for cases where you have lots of collections based code (that doesn't uses generics) and you want to assign it to reference that does. So, the first syntax is allowed.

Henry
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And since Phil said he was using Java 7, this is also correct syntax:


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:This works fine and doesn't give any errors or warnings but this might be because I am using an IDE?

Actually, it should be giving you at least one warning, whether you're using an IDE or not, because the right-hand side (as stated before by others) should be
new ArrayList<>(10)
at the very least.

I'm not trying to be difficult but I want to understand what sort of issues I am leaving myself open to by using the first item below rather than the second.

You'll be opening yourself to a lot more issues if you don't use the 2nd style, because that's how generics works.

Winston

BTW: Class names should always start with a CAPITAL letter.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don’t have <Foo> or <> on the right half of that declaration, there is a risk that somebody might put something in that List which isn’t a Foo.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you don’t have <Foo> or <> on the right half of that declaration, there is a risk that somebody might put something in that List which isn’t a Foo.


But in this example you are adding to "list" which you defined without a type on either side of the declaration - I understand that if you do this then there is no contract on the contents of "list" but if you try to add a Bar object to "flist" in your example will it let you? In this code (apologies to Winston for the lack of proper capitalisation - I'll take that one on board) it allows me to add the dummyListClass object (dl) but gives a compilation error when I try to add the dummyListClass2 object (dl2) so it looks like the contract is enforced:




@Henry Wong:
Simply, the second syntax is the correct syntax. The first syntax is allowed for backward compatibility purposes. Obviously, using the first syntax as written in your example is silly -- as why would you want to have the right hand side of an assignment use the old syntax?


Don't get me wrong, I'm not questioning the accuracy of the response Campbell gave me - I'll take the argument from authority from any of you guys on here - what I'm trying to do is work out why the second syntax is required (I accept that it is) because I don't just want to know what is right I want to understand why it is necessary - that's got to be better in the long run right?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:it allows me to add the dummyListClass object (dl) but gives a compilation error when I try to add the dummyListClass2 object (dl2) so it looks like the contract is enforced:

In your particular case I suspect you're right; it just seems an odd (and inconsistent) way to do it. Josh Bloch reckons that you should always strive to produce code that is both error and warning free; and in the case of generics, it's a very good rule to follow. Warnings (which are usually of the 'requires unchecked cast' variety) are often a sign that your design isn't quite right; and in the above case, not including a type on the right-hand side seems a silly way to save yourself a few keystrokes.

Winston
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Phil English wrote:it allows me to add the dummyListClass object (dl) but gives a compilation error when I try to add the dummyListClass2 object (dl2) so it looks like the contract is enforced:

In your particular case I suspect you're right; it just seems an odd (and inconsistent) way to do it. Josh Bloch reckons that you should always strive to produce code that is both error and warning free; and in the case of generics, it's a very good rule to follow. Warnings (which are usually of the 'requires unchecked cast' variety) are often a sign that your design isn't quite right; and in the above case, not including a type on the right-hand side seems a silly way to save yourself a few keystrokes.

Winston


I take your point; as I said above I'll happily use the standard syntax in future I just thought I must be missing something with the second generic specification.

I also take your point about saving keystrokes - I didn't write the original code like that out of a desire to save keystrokes it was just a mistake.

On the subject of errors and warnings: where should I be looking for these? I get nothing in the output screen and no compiler warnings/errors (net beans IDE).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:On the subject of errors and warnings: where should I be looking for these? I get nothing in the output screen and no compiler warnings/errors (net beans IDE).

That surprises me a lot; however, it's a long time since I used Netbeans. In Eclipse, lines that generate compiler errors are flagged with a red marker on the left, warnings with a yellow marker, and by default it 'compiles on the fly'. It's possible that you have to set that option specifically in Netbeans.

Regardless, you should always see compiler errors on the console if you attempt to run the code.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!