views:

71

answers:

3

Hello,

I'm trying to create a class that can instantiate arrays at runtime by giving each array a "name" created by the createtempobjectname() method. I'm having trouble making this program run. I would also like to see how I could access specific objects that were created during runtime and accessing those arrays by either changing value or accessing them. This is my mess so far, which compiles but gets a runtime exception.

import java.lang.reflect.Array;

public class arrays
{
    private static String temp;
    public static int name = 0;
    public static Object o;
    public static Class c;

        public static void main(String... args)
        {
            assignobjectname();
            //getclassname();//this is supposed to get the name of the object and somehow 
                             //allow the arrays to become updated using more code?
        }
        public static void getclassname()
        {
            String s = c.getName();
            System.out.println(s);
        }
        public static void assignobjectname()//this creates the object by the name returned
        {                                    //createtempobjectname()
             try
             {
              String object = createtempobjectname();
                c = Class.forName(object);
                o = Array.newInstance(c, 20);
             }
             catch (ClassNotFoundException exception)
             {
                exception.printStackTrace();
             }
        }
        public static String createtempobjectname()
        {
            name++;
            temp = Integer.toString(name);

            return temp;
        }
}
+3  A: 

Create a Map then you can add key/value pairs when the key is your name and the value is your array.

camickr
A: 

I expect you're getting a ClassNotFoundException from this line:

c = Class.forName(object);

The value of object the first time it's called is "1", which is not a valid class name.

Class.forName requires a class name as input, such as "java.lang.Integer". Trying to "name" your array in this way doesn't make sense to me. You need to pick an appropriate Java class name.

If you want to "name" an array instance (after you've created it), you could always store the instance as the value in a Map, using the name as the key.

Ash
Sorry for my lack of experience but I'm sorta new to Java and have never even used a map. I'm wondering if you could give me an example.
scriptingalias
A: 

Following up from @Ash's answer, here is some illustrative code. Notice that there is no reflection involved.

Map<String, Object> myMap = new HashMap<String, Object>();
...
Object myObject = ...
myMap.put("albert", myObject);  // record something with name "albert"
...
Object someObject = myMap.get("albert");  // get the object named "albert"
// get("albert") would return null if there nothing with name "albert"

EDIT I've edited the example to use the type Object, since that is more closely aligned with what you are trying to do (I think). But you could use any type instead of Object ... just replace the type throughout the example. And you can do the same with an ArrayList; for example:

List<Date> dates = new ArrayList<Date>();
dates.add(new Date());
Date firstDate = dates.get(0);

Notice that no typecasts are required.

Stephen C
Do I need to convert the object named "albert" to a string if it stored a string initially (usually this is required for an ArrayList which only stores an object and "reading" from it requires casting)? How would I go about writing/reading "albert", or is that what myMap.get("albert") does? Thanks for the help so far, I have no cookies to give since I have no reputation.
scriptingalias
I see now, well I guess that solves the thread. How do I indicate that?
scriptingalias
You should mark your question as answered by choosing the most helpful answer as solution and vote up answers that helped you to understand and solve your problem.
codescape