Results 1 to 6 of 6
Thread: Inserting values into the array
- 07-12-2012, 01:08 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Inserting values into the array
Hi im trying to insert a value into the array and expand it when the array gets full
when i run the program it won't insert anything into the array and shows all nullJava 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; }
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
- 07-12-2012, 01:24 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Inserting values into the array
This looks a bit off:
Shouldn't moving the array located in the variable tempArray to the variable array be done after the for-loop?Java Code:for (int i = 0; i < size; i++) { tempArray[i] = array[i]; array = tempArray; }
- 07-12-2012, 09:46 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Re: Inserting values into the array
You're about to reinvent the wheel; it has been done in the ArrayList class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-12-2012, 09:53 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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
- 07-12-2012, 10:09 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Inserting values into the array
awinston has identified your problem, but this:
should just be an 'else'. Save you doing whatever full() does twice. At this point you know that full() is false.Java Code:else if (!full())
Please do not ask for code as refusal often offends.
- 07-12-2012, 10:21 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
Inserting new values into Array after inital popluation
By rholmes464 in forum New To JavaReplies: 7Last Post: 06-18-2011, 09:14 PM -
Problem in inserting values into Oracle DB
By techsing14 in forum Java ServletReplies: 4Last Post: 04-13-2011, 01:52 AM -
inserting values in to array
By Dayanand in forum New To JavaReplies: 8Last Post: 03-05-2011, 04:23 PM -
inserting values from jtable into database
By sandeepsai17 in forum New To JavaReplies: 1Last Post: 06-29-2009, 07:31 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks