开发者

Java: creating objects of arrays with different names at runtime and accessing/updating them

开发者 https://www.devze.com 2022-12-25 06:44 出处:网络
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

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 accessi开发者_运维知识库ng 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;
        }
}


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消