• Post Reply Bookmark Topic Watch Topic
  • New Topic

missing elements in array  RSS feed

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm populating an array of type URL, printing its contents to the console so I can see them. Then I pass the array as a parameter to a method that is to sort through it and remove duplicate URLs.



This one is a mystery. I've not seen this before. The array prints out the elements to the console just fine prior to the call to removeDuplicateDomains(uts);. I have some println statements inside the method removeDuplicateDomains(URL urls), and as soon as I try to do so each element is blank! Any ideas as to what would cause that?

Alan
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that you're asking a question about why the removeDuplicateDomains method does something or other, am I right? If so then it would help if we could see the source code for that method.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you claim that when you call:

uts has length > 0 and has non-null elements, but then inside removeDuplicateDomains(), when you try to print the elements of uts, its length is 0 or they're all null?

Not possible.

So in one of those two places, you're not looking at what you think you are. A quick scan of your code looks like you are indeed populating the array, and your print loop prior to the method call seems to support that, so it would seem your problem is in the method code, which you're not showing us.

Also, it's never necessary to do println(x.toString()). Simply println(x) is sufficient. The println() method has a signature that takes Object and calls that object's toString() for us.
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I enter method removeDuplicateDomains(URL[] urls) I do check the length of the array being passed. It has 11 elements, which is what it had before I called the method. But when I do the println statement on the first and subsequent elements, they return nothing. Here's the method:



Hope this helps. Knowing you guys you'll probably spot something simple that I'm not seeing. Just need another pair of eyes to show me where I went wrong.

Alan
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not being clear and precise about exactly which code is producing exactly what results, and that's kind of a lot of code to look at. An SSCCE would be better. However, after a quick scan, I see you printing some URLs' paths, not the URLs themselves, but yet you seem to be saying that the URLs are empty.

Try this:
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...this is very interesting. I thought that class URL's getPath() method would return the same string as its toString() method. Not so. Everywhere I had written getPath() it returned an empty string. Comparisons I was making in my method were on empty strings. When I replaced getPath() with just toString() everything fell into place. I don't know what's up with the getPath() method. I don't understand why it doesn't return the same string as the toString() method?

Alan
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Shiers wrote:I don't know what's up with the getPath() method. I don't understand why it doesn't return the same string as the toString() method?Alan


Well, that's because the "path" of a URL isn't the entire URL, as you appear to be assuming. The API documentation for the URL does mention what it actually is. Check it out.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Shiers wrote:I don't know what's up with the getPath() method.


Nothing is up with it. It's doing exactly what it is supposed to do, what it is documented to do.

I don't understand why it doesn't return the same string as the toString() method?


Why would you think that it should?

If I can get on my high horse for a minute here, there are a few widely applicable lessons that can be learned here that are far more important than the simple toString() vs. getPath() solution to this particular problem.

1) Debugging approach. If you add copious println() statements or use a debugger, you'll be able to see exactly what's going on firsthand, and see where your assumptions were wrong, without having to resort to a forum.

2) Communicating the problem. Looking back at your original post, you said, basically, "In this code, everything is fine, but in the method that this code calls, which I'm not showing, there's a problem." Obviously it's more useful to post the code where the problem is occurring than the code where it isn't.

3) Documentation. Read it. 'Nuff said.

Note that I'm not trying to bust your chops here. I'm simply pointing out some things that I consider very important as you continue your programming efforts, but that often get lost among the minutiae of "what do I type to make this work?"

Good luck!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!