Results 1 to 9 of 9
- 02-22-2011, 03:33 PM #1
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
Arbitrary number of objects, each with a unique name.
I need to generate an arbitrary number of objects, and each one has to have a unique name. There has to be a better way of doing this than creating a massive arraylist of objects, all typed out by hand... There could potentially be a hundred!
It doesn't seem like there is any way of assigning the value of a String variable to the Object variable name, I'm quite stuck here...
-
Why do the objects need to have a "name"? What is the basis for this requirement? why not just have an ArrayList of your class of object?
- 02-22-2011, 05:02 PM #3
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
Hmm, Perhaps I should have used the word 'instance'? I'm not sure. What I am trying to do is create a notification for each item in a resultset by calling a custom method that creates and displays notifications based on the Notification class. Some notification class methods are not static, so the importan part of this notification producing method looks like:
the issue is that I need to assign a new variable for Notification each time, else NotificationManager overwrites the old one.Java Code://stuff... while (resultset.hasNext() { createNotification(resultset.getString(1)); } private void createNotification(String string) { //stuff Notification note = new Notification(); note.setTitle(string); note.setText(string + " has returned a result"); NotificationManager.display(note); }Last edited by providence; 02-22-2011 at 05:13 PM.
-
I'm not familiar with your Notification class -- is it one that you've created or is it part of the core Java API? If you want to create a bunch of objects and associate them with a String, and retrieve them based on this String (perhaps the String passed into your createNotification method), then perhaps you could use a HashMap<String, Notification>, and put the String as key and Notification object as value in the map.
- 02-22-2011, 11:53 PM #5
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
Yes, using a hashmap is an option that I've explored, however that requires creating the maximum potential number of objects and placing them all in the map. What I was looking for was a way to create them on a need basis. The issue is that I cannot find a way to assign the value of a String variable as the variable name for the new object. Below is an example of what I want to do, this code won't actually work though..
After looking into the matter some more, I realised that this is fundamentally impossible because of the way the parser/lexar work. So now I'm wondering if there is a way to instruct the parser to evaluate, back up, and use the result of the evaluation as the variable to assign as the name of the Notification instance on the level of the Java code.Java Code:public void someMethod() { //stuff while (resultset.hasNext()) { createNotification(resultset.getString(1)); } } private void createNotification(String objName) { Notification (get the value of objName) = new Notification(); (the value of objName).setText("some text"); //etc... }
- 02-23-2011, 12:01 AM #6
Since you create the Notification object inside the createNotification method it is only accessible inside that method and gets GC when you leave you can call it whatever you like. Unless you store it in a data structure but if you do that there is no need for a "name". Either way I feel you are working towards a solution that does not have a problem.
- 02-23-2011, 12:28 AM #7
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
That would usually be the case, but I'm working with android, and because the notification is displayed outside of any one application, (on the home screen), it is preserved and spared gc-ing, which is the root of my problem. Although hopefully, the case is that I am looking to tackle a problem at the root that can be taken from the stem.
Last edited by providence; 02-23-2011 at 12:31 AM.
- 02-23-2011, 01:07 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I'm not sure why you think a HashMap would require any such thing. You can certainly create your instances as you need them, associate them with a String, and store them in a HashMap as you go. I would strongly encourage you to reconsider this option, and if you run into roadblocks, bring them to us for help. From what you've described, a HashMap would seem to be ideal.
-Gary-
- 02-23-2011, 01:12 AM #9
Member
- Join Date
- Oct 2010
- Location
- Canada
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
gradients between two arbitrary colors
By falkon114 in forum New To JavaReplies: 4Last Post: 02-09-2011, 05:08 PM -
Set not working '-' as unique string
By smritigarg in forum Java 2DReplies: 1Last Post: 01-14-2011, 09:49 AM -
Unique personal number
By Lidiya in forum Advanced JavaReplies: 9Last Post: 12-27-2010, 12:13 PM -
Creating an arbitrary number of variables
By theodorekon in forum New To JavaReplies: 1Last Post: 04-15-2010, 06:10 PM -
Create a VeryLong class that will store an integer of arbitrary length.
By hey in forum New To JavaReplies: 2Last Post: 12-12-2007, 05:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks