It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

help! inserting a new element into a sorted array

help! inserting a new element into a sorted array

Postby jimmy_24 on Wed Nov 15, 2006 8:22 pm

I am having difficulty with a question in an assignment.

in the class, we are managing booklists objects. which are essentially arrays that hold book objects (book class is given to us).

we are supposed to write an insert() class where i need to insert a book into the array, and i already have a method which compares the book to the others and gives me a positive, 0, or negative number if the book is after, in the position, or before the book it is being compared to respectively.
(and the list is already sorted alphabetically so i don't need to do sort it).

the problem i have is actually inserting the book in the required position and "pushing" all the other books forward one space in the array.
the other problem is that once the array is full i need to transfer everything to a bigger array - twice it's size..

and of course - we're ment to do this without using arrayList, but normal arrays.
also - this is an introductory course so we're not really ment to use anything we haven't learned so far. we covered the for, while, do-while loops and general conditional statements and arrays. and we're not allowed to use any array methods.
any help with this will be GREATLY apreciated.

i wrote a single line and don't know where to go from there.
i'm completely new in programming

Code: Select all
   public void insert(Book book) {
      
      for (int i = 0; i < book.compareTo(bookList[i]); i++)
jimmy_24
 
Posts: 0
Joined: Wed Nov 15, 2006 8:17 pm

Who is online

Users browsing this forum: No registered users and 4 guests