Results 1 to 6 of 6
Thread: Inserting values into the array
- 07-12-2012, 02: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
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; }
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, 02:24 AM #2
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 9
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; }
- 07-12-2012, 10:46 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Inserting values into the array
You're about to reinvent the wheel; it has been done in the ArrayList class.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 07-12-2012, 10: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, 11:09 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Inserting values into the array
awinston has identified your problem, but this:
Java Code:else if (!full())
Please do not ask for code as refusal often offends.
** This space for rent **
- 07-12-2012, 11: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, 10:14 PM -
Problem in inserting values into Oracle DB
By techsing14 in forum Java ServletReplies: 4Last Post: 04-13-2011, 02:52 AM -
inserting values in to array
By Dayanand in forum New To JavaReplies: 8Last Post: 03-05-2011, 05:23 PM -
inserting values from jtable into database
By sandeepsai17 in forum New To JavaReplies: 1Last Post: 06-29-2009, 08:31 PM
Bookmarks