Making New Objects...
Started by
lordSauron
, Apr 07 2005 08:09 PM
14 replies to this topic
#1
Posted 07 April 2005 - 08:09 PM
I want to make a new object (it really doesn't matter what object, since I don't have the exact problem in front of me at the moment, but that doesn't matter...), but name it the value of some string or variable that the program can change dynamically. Don't worry about recalling the object - that's the user's problem.
The reason is that I want to create a virtual school scheduling system, with students and teachers that have data like classes and their grades, GPA, and all sorts of rot. However, I want to not store the name in a variable, since I want to have each person be his or her own object. I was told by a friend that I'd need a scripting language like Python to do this, but I think there's a easier way. Does anyone know? I think it has to do with primitive data types, but my vauge guess really does show how clueless I am.
Any help you could give would be great
The reason is that I want to create a virtual school scheduling system, with students and teachers that have data like classes and their grades, GPA, and all sorts of rot. However, I want to not store the name in a variable, since I want to have each person be his or her own object. I was told by a friend that I'd need a scripting language like Python to do this, but I think there's a easier way. Does anyone know? I think it has to do with primitive data types, but my vauge guess really does show how clueless I am.
Any help you could give would be great
Lord Sauron the Great
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
#2
Posted 07 April 2005 - 08:20 PM
i might be missing the point? but you could sace the name to file...?
no point making your program so simple a dummy can use it cause the universe just keeps making a better dummy!
#3
Posted 08 April 2005 - 10:45 PM
No, like this:
CODE
Object <user-input dependant object-name here>=new Object();
Thanks for your time though!
Lord Sauron the Great
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
#4
Posted 09 April 2005 - 07:13 AM
I would store the objects in an array and find an object with an attribute that the user can input.
for example:
for example:
CODE
class SuperObject{
String name;
public getName(){
return name;
}
public setName(String name){
this.name = name;
}
}
String name;
public getName(){
return name;
}
public setName(String name){
this.name = name;
}
}
"Many are persistently in pursuit of the way they have chosen; few in pursuit of the goal"
#5
Posted 09 April 2005 - 04:17 PM
i would store the objects in a hashmap and have the key for it as the object name
#6
Posted 10 April 2005 - 04:02 AM
Hashmap? What's that?
I've heard stuff about hashmaps, but I still have no clue what one is. Anyone care to enlighten me?
I've heard stuff about hashmaps, but I still have no clue what one is. Anyone care to enlighten me?
Lord Sauron the Great
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
#7
Posted 10 April 2005 - 06:39 PM
QUOTE(lordSauron @ Apr 10 2005, 04:02 AM)
Hashmap? What's that?
I've heard stuff about hashmaps, but I still have no clue what one is. Anyone care to enlighten me?
I've heard stuff about hashmaps, but I still have no clue what one is. Anyone care to enlighten me?
its a data structure where you store an object alongside a key
how exactly they work i dont know , i just use them :)
you need to import java.util.HashMap;
then Hashmap<keytype,objecttype> blah = new Hashmap<keytype,objecttype>();
the keytype can be a string for instance, and the object type can be anything you want
Then :
blah.put(key,object);
and blah.get(key); to get/set etc.
theres more functions then that to it ofc , http://java.sun.com/j2se/1.4.2/docs/api/ja...il/HashMap.html will be of use :)
edit : above code works for jdk1.5 , for 1.4 you'll probably have to remove the <> 's and just cast the data types as they are retrieved.
#8
Posted 10 April 2005 - 07:07 PM
Thanks. It'll still probably take a couple weeks for me to get it down - that looks like a pretty complex subject. It looks comparable to an ArrayList, however, in its general capabilities, though it's handleing looks like it can only accept one type of object. Am I right?
Lord Sauron the Great
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
#9
Posted 10 April 2005 - 07:20 PM
QUOTE(lordSauron @ Apr 10 2005, 07:07 PM)
Thanks. It'll still probably take a couple weeks for me to get it down - that looks like a pretty complex subject. It looks comparable to an ArrayList, however, in its general capabilities, though it's handleing looks like it can only accept one type of object. Am I right?
it should be able to do the same as an arraylist for the types of objects you can insert into it
#10
Posted 13 April 2005 - 01:12 AM
I've been doing my homework (for once...) and have arrived at this problem: can I add stuff to a hashmap once it's been created, you know, without having to re-make it with the new stuff? I looked for a way to do that, but it's all so foriegn to me that anything short of something like a method called add(<my data here>) would totally pass me by. Thanks for your help!
Lord Sauron the Great
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
lordSaurontheGreat@gmail.com
JDK 6 Documentation - READ IT! LOVE IT! LIVE IT!
THOU SHALT GOOGLE BEFORE POSING THY QUERY!
If you're reading this you have given me control of your mind.
Why do accesses to your mind throw a "null pointer exception?"
#11
Posted 13 April 2005 - 01:58 AM
QUOTE(lordSauron @ Apr 13 2005, 01:12 AM)
I've been doing my homework (for once...) and have arrived at this problem: can I add stuff to a hashmap once it's been created, you know, without having to re-make it with the new stuff? I looked for a way to do that, but it's all so foriegn to me that anything short of something like a method called add(<my data here>) would totally pass me by. Thanks for your help!
yes
think of a hashmap like a series of boxes. an infinite number of boxes , each with their own name-tag ( key) , and stuff in the box ( data )
you can put new stuff into the box, if you want to - which would remove what was in it before, and add stuff to new boxes whenever you want. You just cant have two boxes with the same name-tag.
you can also clear a box out, and change its nametag
if that made sense :)
#12
Posted 13 April 2005 - 04:30 AM
CODE
dynamicObjects = new Hashtable();
dynamicObjects.put(<user-input dependant object-name here>, new lordSauron());
... code ...
dynamicObjects.get(<user-input dependant object-name here>);
this will reterive the object as an Object, so you should cast accordingly:
CODE
lordSauron dData = (lordSauron)dynamicObjects.get("first_object");
no point making your program so simple a dummy can use it cause the universe just keeps making a better dummy!
#13
Posted 13 April 2005 - 11:09 AM
Summing up basically what jeef posted :
Im using Generics here, from JDK1.5 but if you`re using 1.4 you dont need to - simply cast the output
For this instance, i will be using an Integer object as the 'name tag' , and a String for the 'data'.
Creating a HashMap :
JDK1.4 :
HashMap datastorage = new HashMap();
JDK1.5 :
HashMap<Integer,String> datastorage = new HashMap<Integer,String>();
Adding to a HashMap :
JDK1.4 + 1.5 :
// add two things to the hashmap
Integer myint = new Integer(12345);
datastorage.put(myint,"one two three four five");
// add second
myint = new Integer(98765);
datastorage.put(myint,"nine eight seven six five");
Retrieving from a HashMap :
JDK1.4 :
Integer searchfor = new Integer(12345);
System.out.println("retrieved : " + (String)datastorage.get(searchfor));
JDK 1.5 :
Integer searchfor = new Integer(12345);
System.out.println("retrieved : " + datastorage.get(searchfor));
Checking for a specific key :
JDK 1.4+1.5 :
boolean isthere = datastorage.containsKey(new Integer(12345));
Checking for a specific value :
JDK 1.4+1.5
boolean isthere = datastorage.containsValue("one two three four five");
Removing an entry
JDK 1.4+1.5 :
Integer removethis = new Integer(98765);
String returned = datastorage.remove(removethis);
System.out.println("removed : " + returned);
Getting the number of entries
JDK 1.4 + 1.5
int entries = datastorage.size();
Clearing the entire hashmap
JDK 1.4+1.5
datastorage.clear();
Hopefully that should be all the uses you will require from a hashmap covered, there are other functions to return data as a collection or a set but i dont think you'll need them.
Im using Generics here, from JDK1.5 but if you`re using 1.4 you dont need to - simply cast the output
For this instance, i will be using an Integer object as the 'name tag' , and a String for the 'data'.
Creating a HashMap :
JDK1.4 :
CODE
HashMap datastorage = new HashMap();
JDK1.5 :
CODE
HashMap<Integer,String> datastorage = new HashMap<Integer,String>();
Adding to a HashMap :
JDK1.4 + 1.5 :
CODE
// add two things to the hashmap
Integer myint = new Integer(12345);
datastorage.put(myint,"one two three four five");
// add second
myint = new Integer(98765);
datastorage.put(myint,"nine eight seven six five");
Retrieving from a HashMap :
JDK1.4 :
CODE
Integer searchfor = new Integer(12345);
System.out.println("retrieved : " + (String)datastorage.get(searchfor));
JDK 1.5 :
CODE
Integer searchfor = new Integer(12345);
System.out.println("retrieved : " + datastorage.get(searchfor));
Checking for a specific key :
JDK 1.4+1.5 :
CODE
boolean isthere = datastorage.containsKey(new Integer(12345));
Checking for a specific value :
JDK 1.4+1.5
CODE
boolean isthere = datastorage.containsValue("one two three four five");
Removing an entry
JDK 1.4+1.5 :
CODE
Integer removethis = new Integer(98765);
String returned = datastorage.remove(removethis);
System.out.println("removed : " + returned);
Getting the number of entries
JDK 1.4 + 1.5
CODE
int entries = datastorage.size();
Clearing the entire hashmap
JDK 1.4+1.5
CODE
datastorage.clear();
Hopefully that should be all the uses you will require from a hashmap covered, there are other functions to return data as a collection or a set but i dont think you'll need them.
#14
Posted 13 April 2005 - 09:38 PM
whats the difference between Hashmap and Hashtable? i always use Hastable...
no point making your program so simple a dummy can use it cause the universe just keeps making a better dummy!
#15
Posted 13 April 2005 - 10:07 PM
QUOTE(jeef @ Apr 13 2005, 09:38 PM)
as far as i can tell, from javas site. hashmap is a hashtable with a Map interface bolted on top, and permits null values and null keys.
plus the keys dont have to implement the hashCode method.
Hashtable is synchronised, HashMap isnt.
http://java.sun.com/j2se/1.3/docs/api/java/util/HashMap.html
http://java.sun.com/j2se/1.3/docs/api/java.../Hashtable.html












