gpt4 book ai didi

Java - 引用数组

转载 作者:行者123 更新时间:2023-11-29 02:58:49 25 4
gpt4 key购买 nike

以下是代码片段:

int[] cdb1 = {2,1,1,5,5};
int[] cbd2 = {3,1,1,2,2,6,6};
int[] cbd3 = {3,2,2,3,3,7,7};
int[] cbd4 = {2,3,3,4,4};
int[] cbd5 = {4,4,4,5,5,6,6,7,7};
String this_cdb = "cdb"+Integer.toString(router_id);
int this_cbd_number = this_cdb[0];

我收到以下错误:需要数组,但找到了字符串 int this_cbd_number = this_cdb[0];

这里的router_id可以是1 2 3 4 或者 5。我知道我将 this_cdb 声明为 String。但是我如何将它引用到正确的数组名称呢?

最佳答案

如果没有反射,您将无法从字符串中引用其他变量/字段/类。你需要封装你的数组,例如在另一个数组或 List 中。示例(假设路由器索引自 1):

int[] cdb1 = {2,1,1,5,5};
int[] cbd2 = {3,1,1,2,2,6,6};
int[] cbd3 = {3,2,2,3,3,7,7};
int[] cbd4 = {2,3,3,4,4};
int[] cbd5 = {4,4,4,5,5,6,6,7,7};
int[][] cdb = {cdb1, cdb2, cdb3, cdb4, cdb5};
int this_cbd_number = cdb[router_id - 1][0];

关于Java - 引用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com