Article View: borland.public.jbuilder.java.language
Article #731Re: list class (long)
From: elrond_III
Date: Tue, 14 Dec 2004 22:52
Date: Tue, 14 Dec 2004 22:52
95 lines
4239 bytes
4239 bytes
Hi Scott!
Scott wrote:
[snip]
> I want to createa class called List that will remember a list of
> strings. It'll (hopefully) have methods that will do the following.
[snip]
> I'd like to be able to sory in alphabetical and something I can use
> this list to maintain an email list. Any help anyone can provide
> would be great. Thanks in advance
It didn't really figured out how much help you really want, because you
said you want to learn Java.
First, to melp me or others who maybe post later some interesting questions:
Do you still want to use the new features of Java 1.5 (like generic
programming)?
Do you have any prior knowledge in programming
Do you study something in this way, do something in school, work with it?
Most important to me ^^: *Do you still know data structures*?
So now a litte help, not to much, say if its not enough, ore didn't
helped at all ;).
You wand a class holding Strings, and doing String compare things.
You want this class to shrink ore increase in size.
Thats not that hard, because you can use a prebuild Java Container, a
*ArrayList* (like a Vector in C++ [there is a Vector in Java too, same
as array list, but older]).
An ArrayList can increase and shrink in size automatically, and has
methods to put data inside [put()] and get data from it [get()].
The items in the ArrayList are stored in the order you put them in.
because a ArrayList shrink and increases by itself, i don't think if you
make it this way, it would make sence to set the size at the beginning.
So your class could look like this:
import java.util.ArrayList;
public class Line {
private ArrayList myWords;
Line() {
myWords = new ArrayList(); // if you want 4: new ArrayList(4);
}
public void add(String s) {
myWords.add(s);
}
public void contains(String s) {
myWords.contains(s);
}
// don't show you all, look in the Java documentation
// on developer.java.sun.com
public String get(int n) {
return ((String) myWords.get(n));
// wow: whats that?
// if you store in an ArrayList until
// Java 1.4.2 the object will have forgotten its
// type, its noe of type object
// So you have to cast to get the right object back
// hoever: in 1.5 you don't have to cast ^^ [generic programming]
}
}
so you would just let your functions deligate to the ones of ArrayList.
There shure are other ways to do so.
If you want to explore more of data structures you may for instance want
to to it as a linked list.
Or you implement it simply with an array, but use a binary search to get
the date.
Like allways there are MANY possibilities to do so ^^
hope this helped
cu
Joerg
--
--------------------------------------------------------------
Elrond_III -==: Hack the World :==-
g:R->R; [a,b] in D(g) and g[a,b] in [a,b] and for all x1,x2 in [a,b]
|g(x1)-g(x2)|<=L*|x1-x2| with L <= 1 then one s in [a,b] exist with
s=g(s)
-"Banachscher Fixpunktsatz"
Technical Details
Message-ID:
<41bf6f1c$0$22342$3b214f66@aconews.univie.ac.at>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!news-fra1.dfn.de!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mailReferences:
<4XHvd.12318$Ae.1427@newsread1.dllstx09.us.to.verio.net>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,3f0cbc3440ab15d9 X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!news-fra1.dfn.de!newscore.univie.ac.at!aconews-feed.univie.ac.at!aconews.univie.ac.at!not-for-mail Date: Tue, 14 Dec 2004 23:52:38 +0100 From: elrond_III <elrond_III@gmx.at> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7) Gecko/20040608 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: list class (long) References: <4XHvd.12318$Ae.1427@newsread1.dllstx09.us.to.verio.net> In-Reply-To: <4XHvd.12318$Ae.1427@newsread1.dllstx09.us.to.verio.net> X-Enigmail-Version: 0.84.1.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 95 Message-ID: <41bf6f1c$0$22342$3b214f66@aconews.univie.ac.at> NNTP-Posting-Host: a-138.dsl.tu-graz.ac.at X-Trace: 1103064860 aconews.univie.ac.at 22342 129.27.232.138 X-Complaints-To: abuse@univie.ac.at