Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Inserting values into the array

  1. #1
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Inserting values into the array

    Hi im trying to insert a value into the array and expand it when the array gets full
    Java Code:
    	public void insert(Comparable T) {
    		if (full()) {
    			Comparable[] tempArray = new Comparable[size + increment];
    			for (int i = 0; i < size; i++) {
    				tempArray[i] = array[i];
    				array = tempArray;
    			}
    			for (int j = 0; j < top; j++) {
    				j = appropriatePosition(j);
    				array[j] = T;
    			}
    		} else if (!full()) {
    			for (int j = 0; j < top; j++) {
    				j = appropriatePosition(T);
    				array[j] = T;
    			}
    		}
    		
    	}
    
    	public int appropriatePosition(Comparable T) {
    		int i = 0;
    		while (i <= top) {
    			if (array[i].compareTo(T) >= 0){
    				count ++;
    				return i;
    			}
    			else
    				i = i + 1;
    		}
    		return top + 1;
    	}
    when i run the program it won't insert anything into the array and shows all null
    my initial size of the array is 5 and it should increment by 5 when the array is full
    since top is not declared i added
    top = array[size - 1] but it gives me nullpointer exception and i don't know what to do

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default Re: Inserting values into the array

    This looks a bit off:

    Java Code:
     for (int i = 0; i < size; i++) {
                tempArray[i] = array[i];
                array = tempArray;
            }
    Shouldn't moving the array located in the variable tempArray to the variable array be done after the for-loop?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Inserting values into the array

    You're about to reinvent the wheel; it has been done in the ArrayList class.

    kind regards,

    Jos
    awinston likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Inserting values into the array

    my professor wants us to know how to program it from the scratch so he won't let us use arraylist

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Inserting values into the array

    awinston has identified your problem, but this:
    Java Code:
    else if (!full())
    should just be an 'else'. Save you doing whatever full() does twice. At this point you know that full() is false.
    Please do not ask for code as refusal often offends.

  6. #6
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Inserting values into the array

    finally fixed the problem with inserting......
    time to move on to deleting the element from the array T.T

Similar Threads

  1. Inserting new values into Array after inital popluation
    By rholmes464 in forum New To Java
    Replies: 7
    Last Post: 06-18-2011, 09:14 PM
  2. Problem in inserting values into Oracle DB
    By techsing14 in forum Java Servlet
    Replies: 4
    Last Post: 04-13-2011, 01:52 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. inserting values from jtable into database
    By sandeepsai17 in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 07:31 PM

Posting Permissions

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