Ian Mcloud

Ranch Hand
+ Follow
since Oct 04, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ian Mcloud

This is what I have come up with:
I know I need to compare the previous node with the newNode, and if they are the same, assign the new node to the next node.

5 years ago
In the add method of the OrderedLinkedList class I am trying to place nodes with the same data items consecutively. As I have it now, the nodes are added in the order they are listed in the Test Harness.

Here is the output:

Sue, Bill, Michael, Someguy, Michael, Carl, Steve, Carl, Sue
Sue, Bill, Someguy, Michael, Steve, Carl, Sue
Sue, Bill, Someguy, Michael, Steve, Carl, Sue, Sue, Bill

Here is what I want:

Sue, Sue, Bill, Michael, Michael, Someguy, Carl, Carl, Steve.....etc.

My test case:



LinkedList



Add method:

5 years ago
I see. So cast it somehow?
5 years ago
"hashset"....And from your suggestions, though I had no idea how to implement it at first. (I'm referring to the comments I left in the code).
5 years ago
Finally figured it out.... But one last thing. Any ideas on how I can include duplicate elements? I've sorted the arrays and managed to find all common elements, but I want multiple values of a common element.
Such as, 28, which appears in only two sets but should be printed twice in the common set.






Ouput:

The following elements were common among all arrays:

[[1, 2, 3, 4, 5, 16, 22, 23, 25, 28]]

Additionally, there were a total of 40 comparisons made between the arrays.

[Ljava.lang.Comparable;@3ee284 <<<<<<---- not sure why this though??
5 years ago
I'm assuming you posted before I submitted my last revision. I also changed the Object arrays to int arrays in the driver. I am now getting the correct elements just not enough comparisons.
5 years ago
Still using the same CommonElements class from above.
5 years ago
Nevermind, figured it out... So here is the final bit. I have the proper elements in the array, but I am not getting the correct value for comparisons. Here is the new driver:

5 years ago
Alright, I'm almost there. Now why in the world am I getting this:

The common elements in the arrays are:
[[1, 2, 3, 4, 16]]
[Ljava.lang.Comparable;@a32b
There were: 3 comparisons made.
Exception in thread "main" java.lang.NullPointerException
at CommonElements.getIndexOfLowest(CommonElements.java:30)
at CommonElements.findCommonElements(CommonElements.java:46)
at Test.main(Test.java:16)




5 years ago
I took out the static and changed the setter to this.comparisons = comparisons;
5 years ago
Lol. Not a problem. I guess it is a little more of an intermediate level problem.

Alright, here is what I have designed. The algorithm in findCommonElements is correct, regardless of BigO runtime; however, I am still stuck on the Comparable common, as I cannot figure out how to assign to it the common elements in object1 and object 2.

First class is my Driver/test harness...



And this is the CommonElements class:



My problem exists where I have input my comments, or so I believe.
5 years ago
True Mike. I've gone back to the drawing board. I now have two classes; a driver and the CommonElements class. I'm using predeclared arrays and the findCommonElements method does one thing and thing only. I'll post it shortly. Thanks guys
5 years ago
This is actually fairly close, because I'm getting the common elements and the number of comparisons, but only for each index. i.e. index 0 in array1 = index 0 in array2.
It is not, however, checking to see if index 1 in array 1 = index 0 in array2. So it's not iterating through each and every index.
5 years ago
By the way, I cannot use Hash tables. More specifically: Each collection will be represented as an array of objects of type Comparable.


5 years ago
Alright, I went to the proverbial drawing board and redid the entire thing.
I realized there are two things I was trying to do, but I was simply not implementing them at all.
For starters, I want to give the user the option to choose how many comparisons they want
to make. They should also be able to enter their own elements.



Also, with long lines are you referring to my text or the code?
5 years ago