Results 1 to 9 of 9
  1. #1
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Adding functions to existing Object

    Hello,

    May be I should have posted this question in the part destinated to beginners. But even if it is a rather basic question. I am not sure that a beginner would know the answer.
    I would like to use the object "list" as deklared in the corresponding java library and add other functions, e.g. partly available in DELPHI:

    For example I would like to have a function as part of list object as follows, using "extends":


    List<String> listname = null;
    listname.add("a=1");
    listname.add("b=2");
    String stringValue = listname.valueByName("a"); ///= "1"
    int indexName = listname.IndexByName("a"); /// = 0


    So when I use the wanted function it finds me the first part of the string (before "=") and gives "1" in case of searching for "a". This is a function integrated in DELPHI ("name", "value") and rather useful for me.

    So the name is "a" having the value "1" in this case.

    I know that there are other possibilities to solve this problem, but what I want is to know how to extend an existing (basic) JAVA object of this kind, use the data of the (list-) Object and add this function or other ones.

    Also I would like to know how to add functions as "no duplicate allowed" and "sorted".

    Thanks very much

    ThommyW

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    It's all there in the API :)
    Trail: Collections (The Java™ Tutorials)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    For what you're trying to do, you'd be better off using a Map than a List, since they already do exactly what you're talking about. But anyway, you can easily extend a generic of a given type.

    Java Code:
    public class Junk extends List<String> {
    ...
    }

  4. #4
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Api

    OK, perhaps I am more or less Newbie concerning the API and not used to program "OO". I know the functions Collection.sort and Sets to avoid duplicates but your hint is a little bit to general. Also, perhaps I have to modify my request:

    I think I would like to establish another class e.g. "extendedList" which should be able to use the methods and properties of the list-Object, but with some functions more.
    I am afraid I need some explications more, even better some code. Still better working code....
    In fact the only thing I know ist that I have to use "extended".
    I don't know how to make the constructor using data of the List object and so on.....


    Thanks

    ThommyW
    Last edited by ThommyW; 03-23-2010 at 03:26 PM.

  5. #5
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Hello Toadaly

    In fact I did some programming using maps but I got more troubling effects I could ever have imagined with hash maps: Nearly everything what could (or even could not) happen, happened despite of all logic.....


    Also this only changes one point: how to create my own map with my own properties....

    Thanks

    ThommyW

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

    Default

    Java Code:
    Map<String, String> map = new Hashtable<String, String>();
    
    map.put("a","1");
    map.put("b","2");
    
    String one = map.get("a");  //"1"
    String two = map.get("b");  //"2"

  7. #7
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Example for extList (still not extended)

    Hello ,

    just an example which doesn't work. I want to built a class "extList" which has all properties of the List-Object but where I can add other functions.
    Naturally the contents of a list object needs to be given to this "extList" to work with and it needs to be returned. May anyone tell me what are the errors in the following code ? Where are standing the "()" for ?


    ----------------------------------------------------------------
    import java.util.AbstractList;
    import java.util.ArrayList;


    public static ArrayList<T> extList<T>(a)
    {
    return new extList(a);
    }

    class extList<T> extends AbstractList<T>
    {

    private final ArrayList a;

    extList(ArrayList<T> list) {
    a = list;
    }

    public T get(int index) {
    return (T) a.get(index);
    }

    public T set(int index, T element) {
    T oldValue = (T) a.get(index);
    a.set(index, element);
    return oldValue;
    }

    public int size() {
    return a.size();
    }

    }
    -----------------------------------------------------------------------------
    I am still not sure if I prefer to use "object" or "String" for the elements of the list... Is there a way to use "object" and use also string functions in case of the object are strings ?
    Thanks

    ThommyW

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

    Default

    I want to built a class "extList" which has all properties of the List-Object but where I can add other functions.
    As I showed above, all you have to do is extend whatever List class you want. 'extend' already gives you all the methods and properties of the class you are extending, and then you can add whatever you want.

    If you are trying to expand a runtime object (rather than a class), that doesn't really make any sense and you can't do it.

  9. #9
    ThommyW is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default ok, but

    Hello,

    I know the function of "extends". My problems are others:

    1. Is it better to create a new class e.g. extList or use the list object directly (and if yes how to overwrite it without getting problems?)

    2. Whatever I do: the code above doesn't work... Does anybody know how to make it working ? Better use "AbstractList" or "List" ?

    I already did some programming in JAVA but I am afraid, this kind of stuff, being totally "OO" is not quite clear to me yet...

    Anybody out there who can correct my code ?????????

    Thanks very much

    ThommyW


    By the way I know how to make a hashmap, thanks, but this is not interesting to me. I got many problems using hash maps.

Similar Threads

  1. Get CSS/ Use existing CSS
    By karthikeyan_raju in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 10-21-2009, 05:24 PM
  2. 2 functions
    By baze7 in forum New To Java
    Replies: 3
    Last Post: 08-14-2009, 04:41 AM
  3. Adding existing items to a project
    By Katherine in forum Eclipse
    Replies: 4
    Last Post: 01-20-2009, 04:23 AM
  4. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM
  5. Adding listener to non-Java object?
    By cruxblack in forum Advanced Java
    Replies: 5
    Last Post: 07-30-2007, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •