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
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