在Java中反射数组

发布于 2024-12-18 05:44:54 字数 1071 浏览 2 评论 0原文

我是 Java 新手,有一个任务是编写一些应用程序。面临一个无法通过的问题:(

问题是通过反射更新数组元素(应用程序根据从文件中读取的字符串应用程序选择公共数组来动态更新):

首先,我已经反射了布尔变量,如下所示:

activity = activityName(activities[i].substring(0,activities[i].lastIndexOf('.', activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object obj = field;  
field.setBoolean(obj, true);

这适用于我很好,但现在我需要使用数组而不是常规变量,并尝试进行如下操作:

activity = activityName(activities[i].substring(0, activities[i].lastIndexOf('.',  activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object field_act = field;  
field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);

并出现异常“参数不是数组”:(

In field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);, field_act is boolean[] 我用 .getField(activity), LMKStorage.currentLmkSlot 获取> 是 int 来确定要设置的数组的哪个位置,“true”是要设置的值。我必须获得 100% 的 field_act 是一个数组,因为我没有非数组静态变量。 到目前为止,

我已经有了学习书籍。但仍然没有尝试搜索任何更新数组元素的示例...对我来说没什么用

I am new in Java and have a task to write some application. Faced one problem which can not pass :(

The issue is to update an array element through reflection (app selecting public array to update dinamicaly depending on string app reading from file):

First, i have reflected boolean variables as follows:

activity = activityName(activities[i].substring(0,activities[i].lastIndexOf('.', activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object obj = field;  
field.setBoolean(obj, true);

And that worked for me well. But now i need to use arrays instead of regular variables, and tried to make as follows:

activity = activityName(activities[i].substring(0, activities[i].lastIndexOf('.',  activities[i].length() - 4)));  
Field field = refClass.getField(activity);  
Object field_act = field;  
field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);

And getting exception "Argument not an array". :(

In field_act.setBoolean(field_act, LMKStorage.currentLmkSlot, true);, field_act is boolean[] i am getting with .getField(activity), LMKStorage.currentLmkSlot is int to determine which position of an array to set and "true" is value to set. The field_act i have to get 100% is an array, because i have not non-array static variables in refClass.

so far i have got studing books i have.But still nothing. Tried to google any examples to update array elements... nothing usefull for me.

Please advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2024-12-25 05:44:54

对于数组,请使用 java.lang. lang.reflect.Array 而不是 java.lang.reflect.Field

Object field_act = field.get(obj);
Array.setBoolean(field_act, LMKStorage.currentLmkSlot, true);

For arrays, use java.lang.reflect.Array instead of java.lang.reflect.Field.

Object field_act = field.get(obj);
Array.setBoolean(field_act, LMKStorage.currentLmkSlot, true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文