Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java generics/ArrayList question.

 
Andrew Gary
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You! I figured it out, appreciate the help.
 
Jeramie Hicks
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5810
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic