Results 1 to 14 of 14

Thread: project

  1. #1
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default project

    ive got a big problem, i dont know how to delete a an element of an array.

    heres my project:
    Address Book Entry

    Your task is to create a class that contains an address book entry. The following table describes the information that an adressbook entry has.

    Attributes/Properties Description
    Name Name of the person in the addressbook
    Address Address of the person
    Telephone Number Telephone number of the person
    Email Address Person's Email address

    For the methods, create the following:
    1. Provide the necessary accessor and mutator methods for all the attributes.
    2. Constructors

    AddressBook

    Create a class address book that can contain 5 entries of AddressBookEntry objects (use the class you created in the first exercise). You should provide the following methods for the address book.
    1. Add entry
    2. Delete entry
    3. View all entries
    4. Update an entry

    AddressBook [] add = new AddressBook[5];

    anyone give me any idea, i know already the methods, accessor and mutator, add entry, delete, view, update are my problems.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Set the element to null. Since it is an array, you can't, per se, delete it. All you can do is set it to null.

  3. #3
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    what would be the code if I use ArrayList?

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, I would use a Vector rather than an array. It already has support for adding, deleting, and modifying entries.

    But if your assignment requires that you use an array, the way you delete an element from an array is to create a new array and copy all the elements except the one you are deleting.

    Java Code:
    AddressBook[] newArray = new AddressBook[oldArray.length-1];
    
    int count = 0;
    for(int i=0; i<oldArray.length; i++) {
      if(i != deleteIndex) {
         newArray[count++] = oldArray[i];
      }
    }
    
    oldArray = newArray;
    ....not including error checking....

  5. #5
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    we can use vector but i dont know how to use that, i read about ArrayList

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then use ArrayList. See the API docs. The methods are pretty self explanatory. Give it a try, and if you don't get it right, post your code and we'll help you correct it.

    P.S. Unless the list will be accessed from multiple threads, use ArrayList, Vector has a lot of unnecessary overhead that only costs performance when it will not be accessed from multiple threads.

  7. #7
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    heres my concept,

    public class Main{
    public static void main(String args[]){
    tester testIt = new tester();
    AddressBook [] add = new AddressBook[5];
    testIt.test();
    JOptionPane.showMessageDialog(add[0].getName());
    }
    }
    //++++++++++++++++++
    public class AdressBook{
    public void setName(String name){
    this.name = name;
    }
    public String getName(){
    return this.name = name;
    }
    public static set countAddress(int count){
    this.count += count;
    }
    public static int countAddress(){
    return count;
    }
    }
    //+++++++++++++++++++++
    import javax.swing.*;
    public class tester{
    ArrayList aL = new ArrayList[5];
    Address [] add = new AddressBook[5];
    public void test(){
    for(int count=aL.size();count<aL.size()+1;count++) {
    aL.add(add[count]);
    String aName = JOptionPane.showInputDialog(null, "Enter your name: ");
    add[count].setName(aName);

    }
    }
    }

    i have a problem when i set the object in an array.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by masijade View Post
    P.S. Unless the list will be accessed from multiple threads,
    Which is why I recommend Vector rather than ArrayList. ;)

    Threads are a way of life now. Embrace them.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    There are various List implementations (and you can write your own). Prefer the one that suits your needs.

    Synchronisation isn't much of a determining factor here as Java's collection framework uses the decorator pattern to allow you get collections that are synchronised or unmodifiable. (See the section on Wrapper Implementations in Sun's Tutorial.)

    Some reasons might remain for preferring the antediluvian Vector class - I think some Swing methods use them instead of generic Lists, and they are useful, still, in J2ME. But quirks like this don't really explain why you still see so much discussion of this relic of Java's past.
    Last edited by pbrockway2; 03-13-2009 at 07:55 AM. Reason: typos ;-(

  10. #10
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks.

    suppose

    Vector<student> aB = new Vector<student>();

    AddressBook [] address = new AddressBook();

    then lets say all addresses address[0]. . . address[5] all have data.

    then I use

    aB.removeElementAt(1)

    why is it that it always delete the address[5] which is element 5 of aB? not the one that I specify.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Could you post a Short, Self Contained, Correct (Compilable), Example?

    The snipplet you posted makes no sense as "AddressBook [] address = new AddressBook();" won't compile.

  12. #12
    bhadz202 is offline Member
    Join Date
    Mar 2009
    Posts
    6
    Rep Power
    0

    Default

    im sorry its

    AddressBook [] address = new AddressBook[5];

    ive got an error on typing

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Don't be sorry - just post a SSCCE.

    > why is it that it always delete the address[5] which is element 5 of aB?

    address[5] is a reference to an AddressBook instance while element 5 of aB is an instance of the student class. These things are simply not the same.

    Do post a SSCCE - something that is short (that is very important) which compiles and shows the problem. By "shows the problem" I mean that when you run the code its behaviour (which you describe) is different from the behaviour you expect or intend (which you also describe). Resist the temptation to diagnose what you think the cause of the problem is (removeElementAt() "deleting" the wrong thing or whatever) until after you have described what the problem is.

  14. #14
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by bhadz202 View Post
    thanks.

    suppose

    Vector<student> aB = new Vector<student>();

    AddressBook [] address = new AddressBook();

    then lets say all addresses address[0]. . . address[5] all have data.

    then I use

    aB.removeElementAt(1)

    why is it that it always delete the address[5] which is element 5 of aB? not the one that I specify.
    Here's how a Vector works. Assuming you are using the "add(<whatever>)" method, ".get(0)" will return the first item you added. However, you can add items at arbitrary locations with other methods. If you are doing that, it's up to you to think very carefully about what you are doing.

    Post some code and explain your problem, prefrably using the 'code' tags so we can read it easily. The better you explain, the better help you will get.

Similar Threads

  1. Project Help
    By XxHEXSORxX in forum AWT / Swing
    Replies: 4
    Last Post: 01-28-2009, 10:01 AM
  2. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 PM
  3. Need help for project
    By rip0904 in forum New To Java
    Replies: 3
    Last Post: 05-15-2008, 03:24 AM
  4. Could anyone help with project?
    By billdara in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 05:05 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
  •