Results 1 to 14 of 14
Thread: project
- 03-10-2009, 09:37 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
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.
- 03-10-2009, 10:15 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 03-11-2009, 05:40 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
what would be the code if I use ArrayList?
- 03-11-2009, 06:24 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
....not including error checking....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;
- 03-11-2009, 07:31 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
we can use vector but i dont know how to use that, i read about ArrayList
- 03-11-2009, 07:43 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 03-12-2009, 04:11 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
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.
- 03-13-2009, 07:36 AM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
- 03-13-2009, 07:53 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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 ;-(
- 03-13-2009, 08:33 AM #10
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
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.
- 03-13-2009, 11:14 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-14-2009, 03:04 AM #12
Member
- Join Date
- Mar 2009
- Posts
- 6
- Rep Power
- 0
im sorry its
AddressBook [] address = new AddressBook[5];
ive got an error on typing
- 03-14-2009, 03:33 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 03-14-2009, 07:05 AM #14
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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
-
Project Help
By XxHEXSORxX in forum AWT / SwingReplies: 4Last Post: 01-28-2009, 10:01 AM -
open existing project project ..
By itaipee in forum EclipseReplies: 1Last Post: 12-28-2008, 08:12 PM -
Need help for project
By rip0904 in forum New To JavaReplies: 3Last Post: 05-15-2008, 03:24 AM -
Could anyone help with project?
By billdara in forum New To JavaReplies: 1Last Post: 03-12-2008, 05:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks