Results 1 to 6 of 6
  1. #1
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Inserting elements into an array

    I have here a piece of code from a larger project that involves several classes- I'll do my best to explain what everything does.

    Java Code:
    protected FindOutput find (String name) {
    		for (int i = 0; i < size; i++) {
    			if (theDirectory[i].getName().equals(name)) {
    				return new FindOutput(true, i);
    			}
    		}
    		return new FindOutput(false, 0);
    	}
    protected void add(String name, String number) {
    		FindOutput fo = find(name);
    		if (size >= theDirectory.length) {
    			reallocate();
    		}
    		for(int i = fo.index+1; i < size; i++){
    			theDirectory[i] = theDirectory[i-1];
    		}
    		theDirectory[fo.index] = new DirectoryEntry(name, number);
    		size++;
    	}
    Basically what's happening is I'm trying to be able to insert an element into a dynamic array so I can put a new element somewhere in the array and it'll just shift everything down one, like so:

    [0] [1] [2] [3] [4] [5]

    to

    [inserted] [0] [1] [2] [3] [4] [5]

    theDirectory is my array, and it's operating just like an ArrayList would (that's what the little block with "reallocate" takes care of). The FindOutput class takes the name of the element to be added (entered by the user), checks whether it's in the array, and returns its info accordingly. fo.index will end up being the index of the array where the new element will be added (in this case it will be 0).

    Now here's where we get to the real issue- I feel like this following part should work, but it does not. The for loop should begin at the index after the one I'm adding to, and should essentially just shift everything one over- index 1 will now point to whatever was at 0, 2 will point to whatever was at 1, and so on. The line after the for loop adds the new element at index 0, and then we increment the size of the array in order to account for shifting everything over and adding an element.

    I'm not sure what the problem is with this, but the result is a null pointer exception that's being thrown in find (line 3 above). This is for a school assignment and my professor has told me there's no problem with find and my issue is that add is incorrect, but I can't figure out why. I'd appreciate a hint or two.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inserting elements into an array

    Why don't you use an ArrayList<T>? It does all that nasty work for you. If you aren't allowed to use it, sprinkle some System.out.println( ... ) statements through your code and see what goes wrong ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inserting elements into an array

    I beleive you only declared the array variable(theDirectory) & you didn't initialize it... hence it throws a NullPointerException.... theDirectory isn't pointing/refering to any array

    Like Jos has said, it would be much simpler if you used ArrayList<T>... that way you would be able to add an element at your prefered location/index

  4. #4
    RoKr93 is offline Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: Inserting elements into an array

    ArrayLists aren't allowed for this assignment. I think I've made at least a little bit of progress on this- because I'm expanding the array, I decided to start moving the elements from the end of it:

    Java Code:
    protected FindOutput find (String name) {
    		for (int i = 0; i < size; i++) {
    			if (theDirectory[i].getName().equals(name)) {
    				return new FindOutput(true, i);
    			}
    		}
    		return new FindOutput(false, 0);
    	}
    	protected void add(String name, String number) {
    		FindOutput fo = find(name);
    		if (size >= theDirectory.length) {
    			reallocate();
    		}
    		for(int i = size; i > fo.index; i--){
    			theDirectory[i] = theDirectory[i-1];
    		}
    		theDirectory[fo.index] = new DirectoryEntry(name, number);
    		size++;
    	}
    This code compiles, thankfully, but instead of doing this:

    [0] [1] [2] [3] to [inserted] [0] [1] [2] [3]

    it does this:

    [0] [1] [2] [3] to [inserted] [3] [2] [1] [0]

    So I feel like I'm close to having the correct code, but when I read through mine, it seems like it makes sense. I start my for loop at index size, which is one bigger than than the space of the final non-null element in the array. Then I assign it the value of the value at index one less than its value. I decrement the index for each iteration of the loop until I reach fo.index, the assigned index of the element I'm adding. By my interpretation, that should just shift everything one spot to the right without reversing the order like it does now. Any ideas on why it might be doing this?

    Thanks.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Inserting elements into an array

    What does reallocate() do?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    100
    Rep Power
    0

    Default Re: Inserting elements into an array

    i have an example on arraylists here animated
    that might help you or arrays[] i mean

Similar Threads

  1. Inserting values into the array
    By gnawk in forum New To Java
    Replies: 5
    Last Post: 07-12-2012, 10:21 AM
  2. Problems inserting 1d array to 2d array grid
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 03:04 AM
  3. inserting values in to array
    By Dayanand in forum New To Java
    Replies: 8
    Last Post: 03-05-2011, 04:23 PM
  4. Need help with inserting a new number into an array
    By Get_tanked in forum New To Java
    Replies: 7
    Last Post: 01-21-2011, 02:36 AM
  5. Replies: 2
    Last Post: 09-06-2010, 01:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •