Results 1 to 9 of 9
- 03-22-2010, 03:30 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
- 03-22-2010, 04:32 PM #2
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
- 03-23-2010, 05:25 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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> { ... }
- 03-23-2010, 03:23 PM #4
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
ThommyWLast edited by ThommyW; 03-23-2010 at 03:26 PM.
- 03-23-2010, 03:30 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
- 03-24-2010, 05:08 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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"
- 03-24-2010, 10:25 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
- 03-25-2010, 01:40 AM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.I want to built a class "extList" which has all properties of the List-Object but where I can add other functions.
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.
- 03-25-2010, 10:21 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 7
- Rep Power
- 0
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
-
Get CSS/ Use existing CSS
By karthikeyan_raju in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 10-21-2009, 05:24 PM -
2 functions
By baze7 in forum New To JavaReplies: 3Last Post: 08-14-2009, 04:41 AM -
Adding existing items to a project
By Katherine in forum EclipseReplies: 4Last Post: 01-20-2009, 04:23 AM -
Adding elements to an Object Array
By aneesahamedaa in forum New To JavaReplies: 4Last Post: 09-07-2008, 03:55 PM -
Adding listener to non-Java object?
By cruxblack in forum Advanced JavaReplies: 5Last Post: 07-30-2007, 02:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks