Article View: borland.public.jbuilder.java.language
Article #744Re: list class
From: Scott
Date: Tue, 21 Dec 2004 14:42
Date: Tue, 21 Dec 2004 14:42
109 lines
3783 bytes
3783 bytes
Joerg,
I pasted below this message the code that I have. I think I did pretty
well on it. The only part I'm currently having an issue with is removing an
item from the array.I want to be able to remove one of the names and have
the other items fill the empty spot. I know that there is a built in
function to do it, but I'd like a literal method that will show me step by
step what it's doing. As I've done with the other methods, like add, and
search, etc. Hope this helps. Thanks for the help.
Scott
public class List {
int count; // how many items are in the list
String[] data; // where I put the items.
//defines the list with having the
//capability of holding 4 items
public List() {
data = new String[4];
count = 0;
}
//defines the list as having "n" items.
public List(int n) {
data = new String[n];
count = 0;
}
//add an item to the list
public void add(String s) {
data[count] = s;
count = count + 1;
String[] tempdirectory = new String[count * 2];
System.arraycopy(data, 0, tempdirectory, 0, count);
data = tempdirectory;
}
//removes an item from the list
public void remove(String s) {
remove(s);
}
//Searches for a String
public void search(String s) {
boolean found = false;
int index = 0;
while (!found) {
if (data[index].equals(s)) {
found = true;
System.out.println("The name " + s + " is in the list");
}
else {
index = index + 1;
}
}
}
//Locates a string and returns the integer index of the item
public int locate(String s) {
for (int index = 0; index < count; index++) {
if (data[index].equals(s)) {
return index;
}
}
return -1;
}
//return the String at position indicated by user
String get(int n) {
return data[n];
}
//displays the contents to a String
public String toString() {
String response = "";
response = response + " A list with " + count + " items\n";
for (int i = 0; i < count; i++) {
response = response + data[i] + "\n";
}
return response;
}
//main method that will display the actual contents
public static void main(String[] args) {
List names = new List();
System.out.println(names);
names.add("Scott");
System.out.println(names);
names.add("Dave");
System.out.println(names);
names.search("Scott");
names.add("Bob");
System.out.println(names);
System.out.println(names.get(2));
names.remove("dave");
System.out.println(names);
}
}
Technical Details
Message-ID:
<zDWxd.12514$Ae.10718@newsread1.dllstx09.us.to.verio.net>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!mpls-transit-01.news.qwest.net!newspeer1.dllstx09.us.to.verio.net!verio!newsread1.dllstx09.us.to.verio.net.POSTED!53ab2750!not-for-mailReferences:
<0Nkwd.12381$Ae.6831@newsread1.dllstx09.us.to.verio.net> <41c82ae7$0$22342$3b214f66@aconews.univie.ac.at>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,62cee13431c73458 X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!mpls-transit-01.news.qwest.net!newspeer1.dllstx09.us.to.verio.net!verio!newsread1.dllstx09.us.to.verio.net.POSTED!53ab2750!not-for-mail From: "Scott" <swurm@suffolk.lib.ny.us> Newsgroups: borland.public.jbuilder.java.language References: <0Nkwd.12381$Ae.6831@newsread1.dllstx09.us.to.verio.net> <41c82ae7$0$22342$3b214f66@aconews.univie.ac.at> Subject: Re: list class Lines: 111 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 Message-ID: <zDWxd.12514$Ae.10718@newsread1.dllstx09.us.to.verio.net> Date: Tue, 21 Dec 2004 09:42:37 -0500 NNTP-Posting-Host: 209.139.23.142 X-Complaints-To: abuse@verio.net X-Trace: newsread1.dllstx09.us.to.verio.net 1103640159 209.139.23.142 (Tue, 21 Dec 2004 14:42:39 GMT) NNTP-Posting-Date: Tue, 21 Dec 2004 14:42:39 GMT Organization: NTT/VERIO