Hi, this is driving me insane. I want to loop over the array and check that the random number that has been generated isn't already in the array before I insert it. I have to use an array. Here is the code:
Thank you for your time ! I hope you can help a confused newbie.
Why don't you use a Set? By the way variables should start with an lower-case character.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Sep 21, 2010
I assume you mean RandGen, so yep fixed that thank you . I have to use an array as its in the requirements, I really don't want to be spoon fed the answer I just want a hint in the right direction, thanks
so how would you, as a human being, look at a bunch of numbers and see if there are any duplicates?
The first steps in writing code is writing it out in English (or whatever natural language you choose) One of the mods will tell you to get a pencil with a BIG eraser and some paper. don't write ANY code until you have all the steps worked out there first.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
You are expecting the above code to do the checking if the random value generated is not in the list? But I dont see its checking in the entire array. Its just comparing the random number generated with one of the values of the array and not the complete array.
There are lot of algorithms around which you can use for searching. Basically you would have to do this::
1. Generate random number
2. Search for it in the array- Use some search algorithm.
3. Insert it at the end of the array.
I hope this is the right direction you were looking for
Ok, this is what I have so far with using binary search, but it is still isn't working because it doesn't seem to return the index element if it it does already occur in the array, it returns the negative values fine, but I need the indexes to start the while loop.
I find this code very hard to read. The single method does a lot of things. A golden rule that I follow is that a method should only do one thing. I would
recommend you to split the method up into smaller methods. Then you'll get a much better overview of how the method works.
Also Arrays.binarySearch() only works on sorted arrays. Otherwise the outcome will be unpredictable.
Agree with Wouter. Use of methods with meaningful names will make it easier to read the code.
Joined: Sep 21, 2010
It confuses me to, haha. I'll change it. When I do try to sort it first thing in the while loop with "Arrays.sort(randArray);", it loses the first 25 elements in the array, so when I print
the contents out its:
while the array is not filled
Step-1: Generate a random number
Step-2: Search for the number in the array
Step-3: If not found- Insert in such a way that the array is sorted after the insertion (may be you can use Insertion sort or other sorting algorithm)
You can set the exit condition of the loop to the size of the declared array.
So you have one outer loop which iterates till your array is filled. Then a search method which returns true or false (or you can use the API). Then a method which will insert the element in the array, keep the array sorted.
Hope am clearer now. If you can break up your code according to the steps, it will make the code easier to read.