aspose file tools*
The moose likes Beginning Java and the fly likes Binary search to add elements into array throws null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Binary search to add elements into array throws null pointer exception

Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Hi,

I am fairly new to Java and require your help. I am currently working on a project for university in which I am supposed to allow the addition, removal, modification etc of entries to a phone directory which are supposed to be of the following format: MORRISON J 2456. So the phone directory is supposed to have entries with three pieces of information: a surname, an initial, and a 4 digit phone extension.

I currently have a text file consisting of 10,000 entries. For the purpose of the first part of the project, we are asked to use an Array and not an ArrayList. I currently have four classes. I don't know if you will be able to help much without me showing more of the code, but this is the part that my question mainly relates to. Please let me know if you need to see the rest of the class and other classes.

What is currently happening is that I have an array of default size 7500 and the array doubles if more entries need to be added. I am using binarySearch in hopes that whenever an entry is added, it is added in the correct position so that once the directory is fully generated, it is alphabetically sorted by surname. However, when I run my main method in my test class, because the array is initially just 7500 nulls, a null pointer exception is thrown. I'm just wondering how I can fix this so that despite the nulls, the binary search will populate the array and when there are elements in the array, it will place them in the correct position so that the array is alphabetically sorted (by surname).



Thank you for your help in advance.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

"Where the entry will go" changes as you build the array. When you go to insert your first entry, there's no way to know where it will ultimately end up.

The first entry will always go at index 0. The second will go at 0 or 1. The third will go into 0, 1, or 2. The Nth will go into one of 0..(N-1).

And before you place the Nth entry at index M, everything that's currently at index M through index N-1 will have to move up one. (For this reason a something like a linked list rather than an array-backed data structure might be more appropriate storage.)

So as you're building up this list, you need to keep track of how many of the slots in your array you've used so far, and only look at those when doing the binary search. If you have inserted N entries so far, then indices 0 through N-1 will be non-null and everything from N onward will still be null.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome Daniel. First of all, who told you that binarySearch() method is used to add elements to an array? Read the documentation of the method carefully and understand what it does. It searches the array for the specified value of the primitive or Object. Read this


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff Verdegan wrote:"Where the entry will go" changes as you build the array. When you go to insert your first entry, there's no way to know where it will ultimately end up.

The first entry will always go at index 0. The second will go at 0 or 1. The third will go into 0, 1, or 2. The Nth will go into one of 0..(N-1).



One doubt Jeff - Isn't it obvious that the second entry will go at index 1. Why do you say that it may go at 0 or 1?
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:Welcome Daniel. First of all, who told you that binarySearch() method is used to add elements to an array? Read the documentation of the method carefully and understand what it does. It searches the array for the specified value of the primitive or Object. Read this


Well, I thought that I need to use binarySearch to figure out which position the new entry needs to go into. If this is wrong, how could I achieve this?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:Welcome Daniel. First of all, who told you that binarySearch() method is used to add elements to an array? Read the documentation of the method carefully and understand what it does.


It tells us where an element is, or, if it's not present, where it would go. This can be used to build a list in sorted order.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:
Jeff Verdegan wrote:"Where the entry will go" changes as you build the array. When you go to insert your first entry, there's no way to know where it will ultimately end up.

The first entry will always go at index 0. The second will go at 0 or 1. The third will go into 0, 1, or 2. The Nth will go into one of 0..(N-1).



One doubt Jeff - Isn't it obvious that the second entry will go at index 1. Why do you say that it may go at 0 or 1?


We're building a list in sorted order. If our first entry is "B", then an "A" for the second entry will go at index 0, but a "C" for the second entry will go at index 1.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:
Mansukhdeep Thind wrote:Welcome Daniel. First of all, who told you that binarySearch() method is used to add elements to an array? Read the documentation of the method carefully and understand what it does. It searches the array for the specified value of the primitive or Object. Read this


Well, I thought that I need to use binarySearch to figure out which position the new entry needs to go into. If this is wrong, how could I achieve this?


No, you're on the right track there. Your mistake appears to be that you're looking at the whole array for the position of the new element, rather than just the part you've populated so far.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:"Where the entry will go" changes as you build the array. When you go to insert your first entry, there's no way to know where it will ultimately end up.

The first entry will always go at index 0. The second will go at 0 or 1. The third will go into 0, 1, or 2. The Nth will go into one of 0..(N-1).



One doubt Jeff - Isn't it obvious that the second entry will go at index 1. Why do you say that it may go at 0 or 1?


We're building a list in sorted order. If our first entry is "B", then an "A" for the second entry will go at index 0, but a "C" for the second entry will go at index 1.


Ohh Ok.. Building a list in sorted order. Missed that part.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:
Daniel Perera wrote:
Mansukhdeep Thind wrote:Welcome Daniel. First of all, who told you that binarySearch() method is used to add elements to an array? Read the documentation of the method carefully and understand what it does. It searches the array for the specified value of the primitive or Object. Read this


Well, I thought that I need to use binarySearch to figure out which position the new entry needs to go into. If this is wrong, how could I achieve this?


No, you're on the right track there. Your mistake appears to be that you're looking at the whole array for the position of the new element, rather than just the part you've populated so far.


Right okay. I'm not expecting you to write the code for me of course but as I said I'm still quite new to java so could you give me a hint as to how I would do that?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:Right okay. I'm not expecting you to write the code for me of course but as I said I'm still quite new to java so could you give me a hint as to how I would do that?


I pretty much laid the steps out for you in my first reply. Is there something in particular there that you don't understand?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Adding an element/Object in an array is to be done by simply assigning the specific index position a valid value. So, if you have Contacts class which has surname, initial and phoneNumber as fields, you can create an array which would store Contacts objects:



And then loop through it to add values:




and so on.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:


and so on.


If you look at the code in the original post, and read the OP's questions, it should be clear that he understands the basics of creating an array and setting its elements' values.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:
Daniel Perera wrote:Right okay. I'm not expecting you to write the code for me of course but as I said I'm still quite new to java so could you give me a hint as to how I would do that?


I pretty much laid the steps out for you in my first reply. Is there something in particular there that you don't understand?


Well, for instance, how would I look at the part I've populated so far and not the whole array? Before the text file with 10,000 entries is read and placed into the array in my test class, the array is just 10,000 nulls. So before the first entry is read from the text file and placed in the array, the array is unpopulated, which means I need to avoid a null pointer exception as well.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:
Jeff Verdegan wrote:
Daniel Perera wrote:Right okay. I'm not expecting you to write the code for me of course but as I said I'm still quite new to java so could you give me a hint as to how I would do that?


I pretty much laid the steps out for you in my first reply. Is there something in particular there that you don't understand?


Well, for instance, how would I look at the part I've populated so far and not the whole array?


You know how to keep track of how many elements you've added so far, right? Since the used array slots will be built of from index 0 up to however many you've added so far (minus 1 due to array indices starting at 0), you just look at indices from 0 up to that limit.

For instance, when we're going to add the next element to this partially built array, we only look at indices 0..3
[5, 11, 12, 19, null, null, ... null]

I need to avoid a null pointer exception as well.


The way you'll avoid an NPE is by never looking at the part of the array that has nulls in it.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:
Daniel Perera wrote:
Jeff Verdegan wrote:
Daniel Perera wrote:Right okay. I'm not expecting you to write the code for me of course but as I said I'm still quite new to java so could you give me a hint as to how I would do that?


I pretty much laid the steps out for you in my first reply. Is there something in particular there that you don't understand?


Well, for instance, how would I look at the part I've populated so far and not the whole array?


You know how to keep track of how many elements you've added so far, right? Since the used array slots will be built of from index 0 up to however many you've added so far (minus 1 due to array indices starting at 0), you just look at indices from 0 up to that limit.

For instance, when we're going to add the next element to this partially built array, we only look at indices 0..3
[5, 11, 12, 19, null, null, ... null]

I need to avoid a null pointer exception as well.


The way you'll avoid an NPE is by never looking at the part of the array that has nulls in it.


Okay I may be way off here so bear with me. I've added some code but I still get a null pointer exception so obviously it's not right but I'll post it so that you can tell me if I'm even remotely close to getting it right, and if I am then what I'm doing wrong.


Thank you
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Which line is causing the NPE?

Add print statements so you can see which elements are being searched and what their values are.

This makes no sense:


You don't need to do that. You know the number of elements. It's already stored in your endOfArray variable, basically. You know that everything after that variable is null, and you know how many elements you currently have based on that variable.

I would actually advise getting rid of endOfArray and making noOfElements a member variable. To me at least, it seems easier and clearer to keep track directly of how many elements I have, and then to array index math relative to that, rather than the other way around. Your call though. Either way, you only need one of those variables, and that loop to count elements is totally pointless.

Otherwise, your code looks pretty close to what it should be. Most likely you have a simple off-by-one error somewhere.

Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:Which line is causing the NPE?

Add print statements so you can see which elements are being searched and what their values are.

This makes no sense:


You don't need to do that. You know the number of elements. It's already stored in your endOfArray variable, basically. You know that everything after that variable is null, and you know how many elements you currently have based on that variable.

I would actually advise getting rid of endOfArray and making noOfElements a member variable. To me at least, it seems easier and clearer to keep track directly of how many elements I have, and then to array index math relative to that, rather than the other way around. Your call though. Either way, you only need one of those variables, and that loop to count elements is totally pointless.

Otherwise, your code looks pretty close to what it should be. Most likely you have a simple off-by-one error somewhere.



Okay well, to be honest I don't see what the difference would be between endOfArray and noOfElements. You said the code above is not necessary and makes no sense, (the noOfElements bit), what about the following bit that I had added:


Also, you asked which line caused the NPE, it is the int x = Arrays.binarySearch(directoryArray, 0, endOfArray, new Entry(initial, surname, ext))*1; line.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:
Also, you asked which line caused the NPE, it is the int x = Arrays.binarySearch(directoryArray, 0, endOfArray, new Entry(initial, surname, ext))*1; line.


I would break that line up.


That way you can see more of what's going on.

Also, was it actually that line that caused the NPE? That one was on top of the stack trace, not something inside binarySearch()?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff, won't it be useful to debug the code using Eclipse? It would help Daniel understand the flow better, rather than putting print statements.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:Jeff, won't it be useful to debug the code using Eclipse? It would help Daniel understand the flow better, rather than putting print statements.


Either approach can be useful. It depends on the individual and the situation.

In this case, I don't know if he's using an IDE, so that may not even be an option. And given that he's a beginner, I recommend against using one anyway.

If he's already using one, and is at least somewhat familiar with the debugger, then sure, it could be used here. But given that he's already having some difficulties with the programming task at hand, I wouldn't recommend trying to learn a new tool at the same time. It will probably add to the confusion more than it will help.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Jeff, won't it be useful to debug the code using Eclipse? It would help Daniel understand the flow better, rather than putting print statements.


Either approach can be useful. It depends on the individual and the situation.

In this case, I don't know if he's using an IDE, so that may not even be an option. And given that he's a beginner, I recommend against using one anyway.

If he's already using one, and is at least somewhat familiar with the debugger, then sure, it could be used here. But given that he's already having some difficulties with the programming task at hand, I wouldn't recommend trying to learn a new tool at the same time. It will probably add to the confusion more than it will help.


Hi again. Well, I am indeed using Eclipse and I do know a little bit about the debugger but not too much. I did go into debug mode and try to figure out what was going on, however as you said it did make me more confused. Here is what I currently have:

When I run the debugger it appears that it adds the first element without any trouble into index 0. What it appears to do when it goes onto the second element it wants to add is overwrite the previous element that was added, and add the second element into index 0 as well. I'm guessing this means I need to shift each element that is present in the array by one before I add the new element into its position. However I'm not entirely sure how to achieve this. I thought of something like this, but it doesn't seem to do the trick, so I must have an error in logic:

Btw, in case it'll help, my print statement return this output in the console before throwing the null pointer exception:

directoryArray=[LEntry;@5e3c58a9
endOfArray=0
entry=SMITH A 0001
1
directoryArray=[LEntry;@5e3c58a9
endOfArray=1
entry=JOHNSON B 0002
1
directoryArray=[LEntry;@5e3c58a9
endOfArray=2
entry=WILLIAMS C 0003

Finally, when you say deal with whether currentLocation is + or -, I'm not sure how I'm supposed to deal with that.

Thank you,

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi again. Well, I am indeed using Eclipse and I do know a little bit about the debugger but not too much. I did go into debug mode and try to figure out what was going on, however as you said it did make me more confused.


Happens Daniel. Can you share the code of the Directory interface as well and the other classes? I would like to try out this for myself and see where exactly is it breaking. Then I shall try and explain it to you.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:
Hi again. Well, I am indeed using Eclipse and I do know a little bit about the debugger but not too much. I did go into debug mode and try to figure out what was going on, however as you said it did make me more confused.


Happens Daniel. Can you share the code of the Directory interface as well and the other classes? I would like to try out this for myself and see where exactly is it breaking. Then I shall try and explain it to you.


Sure. I have quite a few bits commented out and all because I had the program working and then realised I should change some stuff to make it more efficient. Here are all of the classes:

ArrayDirectory class:

Directory interface class:

Entry class:

TestDirectory class:

Thank you


Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

And the text file. I would need that too.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:And the text file. I would need that too.


Sorry forgot that. It doesn't let me attach the file for some reason though, so I'll have to provide a link. Hopefully it's not against the forum rules...

http://www.sendspace.com/file/cpqdsg

Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:
Mansukhdeep Thind wrote:And the text file. I would need that too.


Sorry forgot that. It doesn't let me attach the file for some reason though, so I'll have to provide a link. Hopefully it's not against the forum rules...

http://www.sendspace.com/file/cpqdsg

Thanks


You shouldn't be testing with your full file right now. Start with something MUCH smaller, like 5-10 entries.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Well, it took some work on my part to beat your class into shape. But I was successful in storing the elements in the array.

Right: Lets go step by step. You are reading the entries correctly. The place you seem to be messing up is storing the elements in the array in the addEntry(String, String, String) method:



Another thing I noticed is your overcomplicated compareTo() method:



It would be much simpler to say:



Lets define single sort order first. I don't think you can define multiple sort orders by implementing Comparable<T>.compareTo(T obj). Correct me if I am wrong here Jeff.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:I need to shift each element that is present in the array by one before I add the new element into its position. However I'm not entirely sure how to achieve this. I thought of something like this, but it doesn't seem to do the trick, so I must have an error in logic:


So, if you have 5 elements (indices 0..4), then when you get the 5th, say it belongs at index 3, you'll do this:



Which shifts one entry down, when in fact, you need to shift all entries after a certain point up

(Or else those two lines are already in a loop that you just didn't show, and you're shifting a bunch of entries down and putting your new entry into a bunch of locations.)


Finally, when you say deal with whether currentLocation is + or -, I'm not sure how I'm supposed to deal with that.


binarySearch() returns the position where an entry is (+) or the position where it would go (-). You need to decide how to handle an entry already being present, or else you need to document a precondition that all entries must be unique.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:

Another thing I noticed is your overcomplicated compareTo() method:

It would be much simpler to say:


Lets define single sort order first. I don't think you can define multiple sort orders by implementing Comparable<T>.compareTo(T obj). Correct me if I am wrong here Jeff.


Hi. You said I need a single statement to add the entry and increase array size by one. Do I not need to do binary search to find the position which the element needs to go into like I was doing before so that it is sorted alphabetically by surname and then move elements up by one position after inserting the new entry? I would've thought that would at least be multiple lines of code, no?

Also, you said I should change the compareTo method to the following:

However I need to compare the surnames, not the extensions. Will this be enough? What would happen when the surnames are the same?

Thanks

Jeff Verdegan wrote:


So, if you have 5 elements (indices 0..4), then when you get the 5th, say it belongs at index 3, you'll do this:



Which shifts one entry down, when in fact, you need to shift all entries after a certain point up

(Or else those two lines are already in a loop that you just didn't show, and you're shifting a bunch of entries down and putting your new entry into a bunch of locations.)


Does this make any sense:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:
Another thing I noticed is your overcomplicated compareTo() method:

[snip]

It would be much simpler to say:



Lets define single sort order first. I don't think you can define multiple sort orders by implementing Comparable<T>.compareTo(T obj). Correct me if I am wrong here Jeff.


It's not too overly complicated for what he wants to do. It could be cleaned up a bit, but its behavior is either correct or very close. He obviously has the right idea, so there's no point in going to the single sort case.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Right after my previous post I realised it was stupid to do directoryArray[currentLocation] = entry within the for loop. Does this make any sense:


Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Daniel Perera wrote:
Mansukhdeep Thind wrote:

Hi. You said I need a single statement to add the entry and increase array size by one. Do I not need to do binary search to find the position which the element needs to go into like I was doing before
so that it is sorted alphabetically by surname and then move elements up by one position after inserting the new entry? I would've thought that would at least be multiple lines of code, no?




Your logic is flawed. Understand what you are doing. You are writing :





Right now what you are attempting to do is find the currentLocation of the entry to be added based on the relative position of the surname values of that entry to those already present in your directoryArray[].
Correct? But initially, your array has no elements, or in other words, all elements are null. Then , what are you essentially supplying as a parameter to binarySearch() method is an array of null elements.
You want the API to return the currentLocation as a valid value by comparing its surname with the surnames existing, which are nulls. Hence, it fails inside the binarySearch() in attempting to compare a null and a surname. Are you with me here David?

Read the documentation of Arrays.binarySearch(Object, int, int, Object) carefully. It clearly mentions that the list has to be sorted according to natural order prior to this call. Otherwise results will be undefined. In your case, what is natural order? Which entry would precede which other entry? There is no natural order here as we have an object which is composed of multiple attributes. Are you getting the problem? That is why I suggested sorting it according to one order which could be extension as it is unique for each Entry object.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Daniel Perera wrote:Right after my previous post I realised it was stupid to do directoryArray[currentLocation] = entry within the for loop. Does this make any sense:




Yes, that too and read what I have explained above. Understand why the NullPointerException pops up.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:
Right now what you are attempting to do is find the currentLocation of the entry to be added based on the relative position of the surname values of that entry to those already present in your directoryArray[].
Correct? But initially, your array has no elements, or in other words, all elements are null. Then , what are you essentially supplying as a parameter to binarySearch() method is an array of null elements.
You want the API to return the currentLocation as a valid value by comparing its surname with the surnames existing, which are nulls. Hence, it fails inside the binarySearch() in attempting to compare a null and a surname. Are you with me here David?

Read the documentation of Arrays.binarySearch(Object, int, int, Object) carefully. It clearly mentions that the list has to be sorted according to natural order prior to this call. Otherwise results will be undefined. In your case, what is natural order? Which entry would precede which other entry? There is no natural order here as we have an object which is composed of multiple attributes. Are you getting the problem? That is why I suggested sorting it according to one order which could be extension as it is unique for each Entry object.


I do understand why the NullPointer Exception pops up, I have 7500 nulls initially so I definitely get that that is the root of the problem. With regards to sorting by extension - we are specifically asked in our project specification to sort by alphabetical order of surname... When elements are being added to the array, there are always going to be null values until the array is completely filled up, in which case the array is doubled and another bunch of nulls are present until they are replaced with new entries. I'm not entirely sure as to how I would overcome the comparison of a surname and a null, at which point I once again would ask for your help. I have a feeling getting this to work really shouldn't be taking me this long so I apologise if I am taking up so much of your time, but as I previously stated I am fairly new to Java and some things are not as obvious to me as they should be.

I'm not actually expecting you to give me the answers or write the whole code for me but if you could give me a bit more to go on that would be great.

Thank you
Daniel
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Daniel Perera wrote: .... I'm not entirely sure as to how I would overcome the comparison of a surname and a null, at which point I once again would ask for your help. I have a feeling getting this to work really shouldn't be taking me this long so I apologise if I am taking up so much of your time, but as I previously stated I am fairly new to Java and some things are not as obvious to me as they should be.


No worries. No apologies either. We are all here to happily learn. You do not have to compare a surname with a null. Like Jeff said, you have to look at only that portion of the array that has entries in it. That is the whole point. It is easy to sort an array which is fully populated. But since you are dealing with just in time sorting of a partly populated array, you have to tweak the logic a bit. Let me break down the problem for you. I give you an array of size 5:




Can you sort this array for me? I should get the output as:

Perera
Ritchie
Verdegan


I have the code ready with me. But if I simply provide you with that, you will not learn anything at all. Take a pencil and write down the logic in English that you will be implementing as code later.

Hint: Consider those elements only that are not null.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Mansukhdeep Thind wrote:
Can you sort this array for me? I should get the output as:

Perera
Ritchie
Verdegan



Okay well here's what I've done but I have a feeling I've overcomplicated it somehow. Nonetheless it works.

I was previously using Arrays.sort to sort the array for elements which are not null, however our lecturer said that we should not need to sort the array each and every time we want to print it but instead insert the entries in the correct position relative to other entries within the array each time the addEntry() method is called. Therefore I've decided that I need to use binarySearch like I've been trying to...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Daniel Perera wrote:I was previously using Arrays.sort to sort the array for elements which are not null, however our lecturer said that we should not need to sort the array each and every time we want to print it but instead insert the entries in the correct position relative to other entries within the array each time the addEntry() method is called. Therefore I've decided that I need to use binarySearch like I've been trying to...


Yes, using binarySearch() is the correct approach.
Daniel Perera
Ranch Hand

Joined: Apr 17, 2013
Posts: 47
Jeff Verdegan wrote:
Daniel Perera wrote:I was previously using Arrays.sort to sort the array for elements which are not null, however our lecturer said that we should not need to sort the array each and every time we want to print it but instead insert the entries in the correct position relative to other entries within the array each time the addEntry() method is called. Therefore I've decided that I need to use binarySearch like I've been trying to...


Yes, using binarySearch() is the correct approach.

Jeff, after your last post where you talked about how I was shifting one entry down when in fact I need to shift all entries after a certain point up, I asked if this made any sense but you didn't respond. I thought it was reasonable but it still doesn't work and it appears I'm still not able to deal with the nulls. Could you help?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Binary search to add elements into array throws null pointer exception
 
Similar Threads
Array problem
Help with adding elements to vector
Need help with sorting random array of integers
getting error that main class cannot be found??!
Creating random names and phone extensions, help please!