Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!

Piet Souris

Bartender
+ Follow
since Mar 08, 2009
Forum Moderator
Piet Souris currently moderates these forums:
Netherlands
Cows and Likes
Cows
Total received
155
In last 30 days
1
Total given
104
Likes
Total received
852
Received in last 30 days
15
Total given
94
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piet Souris

A disadvantage is that'.xml' is added to the filename, even if that filename already ends with that extension.

The usual reaction to a file already existing is to pop up a Confirm dialog asking whether it is okay to overwrite. If no, then the whole saving dialog repeats.

An alternative is: if the file already exists, then derive a name that does not exist, and save the lot under that name. Then show a message dialog that mentions the saved name. I once used this code:

I haven't used this code in years, I can't remember if I liked the working of it. And I used some bad names here and there, now I look at it.
3 days ago
I'm certainly no expert when it comes to HTML. My code is based on the example given. So it checks for the empty tag, but a single <br> would currently lead to an 'unclosed opening tag' or 'unmatched opening/closing tags'. Are there tags that have no closing tags? And, even more fun, are there tags for which the closing tag is optional?
4 days ago
Yes, the Collections Framework! I have the impression that that Framework gets  little forgotten, since java 8'th Streams and Lambdas. I mean: when was the last time you saw someone use the beautiful 'retainAll'' and 'removeAll'?
4 days ago

Campbell Ritchie wrote:Is there a method for sorting a Stream by the count of elements matching a particular feature?


Yes, several. In this case you could use a method from the JCF. Here is an example that is very inefficient:
6 days ago
I used a LinkedList<String> called 'tags' where I put the opening tags. As soon as I encounter a closing tag, I check whether that closing tag equals the last element of tags (taking the forward slash into account, of course). If so, I remove the last element of tags, if not, then I report an error and the line number.

In the end my tags-list should be empty. If not, then I print my error report.

Note that there are quite some peculiarities to take care of.
6 days ago
A simple way is for instance:
1 week ago
Well, I did my test with 20_001 vertices and with edges  (i -> i + 1) for i = 0...19_999.

it ran for maxDepth = 4_000, but gave a StackOverflow for maxDepth = 5_000.

And finally: you can do that stepwise recursion also in your original recursion, with some adaptions.
1 week ago

Campbell Ritchie wrote:Why are you using an array of arrays rather than an ordinary array?


creates a 100x100 matrix    

But talking about Streams, you can have Java do all the calculations. For instance:

statistics is of type IntSummaryStatistics, and contains the count, min, max, sum and average.
1 week ago
@OP

in your opening post, you ended with a nice code snippet, using a TreeSet. What happened to that snippet?
1 week ago
Yes, something like:

with getNextVertex delivering the next vertex, setting the status to FINISHED when that vertex is the last one.
2 weeks ago
good afternoon, DJ,

no, reaching maxDepth says nothing about there being a solution or not. If the number of edges is larger than maxDepth, then the rourine simply isn't over yet when reaching maxDepth. And if the number of edges is smaller than maxDepth, then maxDepth will never be reached.

Have you thought about a non-recursive solution, that does not suffer from a too deep recursion level?

Still have to perform my test, I'm in a pretty lazy mood these days (months)
2 weeks ago
hi DJ,

I gave your dfs another look, and I noticed I misunderstood the exact workings. So, forget my remarks, they are nonsense. Well done for coming up with that recursion!

But that means that my way of splitting up the recursion into manageble groups fails, for your dfs requires the full history in order to work.

So I devised a way where that history is not needed, by writing a vertex directly to the ouputpath before going into the recursion. The code is now:

and my dfs looks like:

Have tested it for the vertices 0-1, 1-0, 1-2, 2-1 and for 0-1, 1-0, 2-3, 3-2.

I am stting up a test where we have 20.000 vertices with edges i - (i + 1) for i = 0... 20.000 and a recursion depth of 1000. But that input can't be read from a txt-file, so must break into my code. I let you know!

O ja: my test for connectedness has the same order as the dfs itself, so that is not very useful, it would only double the run time.
2 weeks ago
If OP wanted the sequence 0-99 (or 1-100) in some order, then I don't think OP would have much trouble determining min, max and sum. You guys are missing OP's last code snippet.
2 weeks ago
It is random, it is just like a big lotto, drawing 100 balls out of 500 (drawing without replacement).

@OP
you have a TreeSet with 100 different values. Since the TreeSet uses natural ordering, the smallest value is the first element, the largest value is the last element. Have a look at the API of a TreeSet, and see how you can get the first and last elements.

And to determine the sum: you can use: for (int element: treeset) ...



I have to use this format, because for some reasons I get an error when using normal font (CSFR error, or something like that)
2 weeks ago

Campbell wrote: So why are you using a set?


I guess to ensure 100 distinct random values. How would you change your stream-examples to get the same result?
2 weeks ago