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 Java generics/ArrayList question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Java generics/ArrayList question." Watch "Java generics/ArrayList question." New topic
Author

Java generics/ArrayList question.

Andrew Gary
Greenhorn

Joined: Aug 31, 2011
Posts: 8

I can not figure out why my program works when I use this code

But when I switch out what strings I add to the strings MyList it does not work correctly below is the code that does not work.

Can someone explain why one will work but the other will not?
I have gone over the code for a hour and still can't figure it out.

Thanks in advance.

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Peculiar, since both Integer and String implement Comparable.

The type for Comparable is actually <T extends Comparable<? super T>>

I don't know whether changing that will help.

But you haven't told us what happens. We can't make anything of "it doesn't work". You need to tell us what happens.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You also didn't tell us the difference between the two code samples. What did you change?

By the way: don't use /*...*/ for commenting out code. Use // for each line.
Andrew Gary
Greenhorn

Joined: Aug 31, 2011
Posts: 8

Campbell Ritchie wrote:By the way: don't use /*...*/ for commenting out code. Use // for each line.

I'm just asking this for learning purposes but why do it this way?

And what I changed was the Strings that go into the MyList.
When I use these strings:

The program returns the correct String for the strings.largest() and strings.leastt() methods.

But when I use these strings:

The strings.largest() and strings.least() methods do not return the correct Strings.
I still can not figure out why it is doing this.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

The problem lies here:

You're comparing two adjacent items from the list, and based on that comparison, you set the greatest variable. You need to compare the greatest variable to all items in the list. I'm not going to show you the correct code, try to figure it out yourself.

It sometimes accidentally seems to work. It depends on the ordering of the elements in your list. As a practice, you can try to find out and formulate the conditions which lead to the code actually returning the largest value.

The least() function has the same problem. Fixing it will be very similar to fixing this.
Andrew Gary
Greenhorn

Joined: Aug 31, 2011
Posts: 8

Thank You! I figured it out, appreciate the help.
Jeramie Hicks
Greenhorn

Joined: Sep 01, 2011
Posts: 3
Campbell Ritchie wrote:By the way: don't use /*...*/ for commenting out code. Use // for each line.


Curious about this myself...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

The reason is that you can't nest block comments.

Let's say you want to comment out a piece of code that already contains a block comment somewhere within it. If you use another block comment to comment it out, it will break because the terminating */ of the "inner" comment will terminate the "outer" comment.

Line comments avoid this problem. You'll make the job of another programmer maintaining your code much easier if you use line comments.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Agree. The different sort of comments all have their place, and they are all suitable for different purposes. The line-end comment, applied at the start of a line, is immune from */ appearing anywhere in that line. Some editors and IDEs will automatically comment-out or comment-in several lines if you highlight them and push a certain combination of keys, possibly ctrl-shft-C.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java generics/ArrayList question.