Results 1 to 10 of 10
- 03-05-2011, 06:11 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Help with accessing static method
Hi folks
I have a very simple class that creates two HashSets (one is a copy of the other). Reason I'm doing this is that the second HashSet will perform some further computations that I'm not attempting at this point in time. Anyway, here's the code:
Now, If I create an array if integers using the following:Java Code:import ou.*; import java.util.*; /** * Class Consecutives - A utility class for removing a consecutive pair * of integers from a set of integers. * * @author (M255 CT) * @version (1.0) */ public class Consecutives { /** * A class method to return a set containing the integers that are the * elements of the array provided as the argument. The result can be used * as the actual argument to the removeConsecutive() method). */ public static Set<Integer> numberSet(int[] integers) { Set<Integer> numSet = new HashSet<Integer>(); for (int eachInteger : integers) { numSet.add(eachInteger); } return numSet; } public static Set<Integer> removeConsecutives(int[] numberSet) { Set <Integer>set1 = new HashSet<Integer>(); set1.addAll(numberSet(numberSet)); return set1; } }
I now want to populate my first HashCode with the array values, then execute the second method with the result of the first, sending the following message:Java Code:int[] numbers = {1, 17, 6, 2, 5, 9};
However, I get the following error:Java Code:Consecutives.removeConsecutives(Consecutives.numberSet(numbers));
What am I doing wrong? If I send a message directly to the Class method using the array it works, for example sending the message:Java Code:Semantic error: : Static method removeConsecutives( java.util.HashSet ) not understood by class'Consecutives'
returns the values you would expect.Java Code:Consecutives.removeConsecutives(numbers);
I know this will be something very simple but I'm stumped.
Thanks in advance
-
Your calling the removeConsecutives method by passing a HashSet into its parameter, but as written it can only accept an array of ints. As an aside, I'm not really sure what you're trying to do with this method as it doesn't appear to be removing anything.
- 03-05-2011, 06:30 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Hi Fubarable
Your right is saying that the method at this point isn't doing anything, I still have to complete the code (will be moving onto that later).
The method (as you rightly say) method does accept and array of ints as it's argument which is correct. I will need to provide the argument for the method with a variety of ints that are sent directly. It the argument decleration the reason I'm getting the symantic error?
Thanks
-
Again, you are getting the error because you are not calling the method with an array of ints, that's why.
- 03-05-2011, 06:49 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
-
No, you can't simply cast a Set as an array. Makes no sense. What you need to decide first is what this method is supposed to do.
You are constrained by the method signature which you yourself have created.Indeed, any argument for removeConsecutives needs to be an array?
- 03-05-2011, 07:47 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Hi Fubarable
Think I've got it now, change the method to the following:
Which makes sence now that I think about it. Thanks for your help.:)Java Code:public static Set<Integer> removeConsecutives(HashSet <Integer> newSet) { Set <Integer>set1 = new HashSet<Integer>(); for (int anInt : newSet) { set1.add(anInt); } return set1; }
-
It still doesn't make sense to me. The method name states that it removes consecutive elements, and I don't see anything being removed by this method.
- 03-05-2011, 09:03 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Yes, that's because I've not written that part yet, as per post #one. Perhaps it's just the way I like to tackle these things. I break them down to the smallest components as possible and build up the method from there. If your interested the method will eventually subtract each int within the set from each other int, and when the answer is one (i.e donating consecutive numbers) this will populate another set. Once this new set has been populated, this will be the mechanisim I will use to remove the elements from the original set. Confused? I know I am.
Thanks for your help BTW. It did point me in the right direction. I won't mark this post as solved yet and will post the final code once I've completed it (just in case your interested).
Cheers:D
- 03-07-2011, 09:04 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Fubarable
Thanks once again for your help. As promised here is my completed code. Your comments would be very much appriciated.
ThanksJava Code:public static Set<Integer> removeConsecutives(HashSet <Integer> newSet) { TreeSet <Integer>set1 = new TreeSet<Integer>(); TreeSet <Integer>set2 = new TreeSet<Integer>(set1); for (Integer anInt :newSet) { set1.add(anInt); } for ( Integer i : set1) { if (set1.higher(i) !=null) { if((set1.higher(i) - i) ==1) { OUDialog.alert("Consecutive Numbers " + i + " and " + set1.higher(i) + " have been removed"); set2.add(i); set2.add(set1.higher(i)); } } } set1.removeAll(set2); return set1; }
Similar Threads
-
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
non-static method getType cannot be referenced from a static contex
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-12-2010, 11:05 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks