Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将整数值附加到textview名称?_Java_Android - Fatal编程技术网

Java 如何将整数值附加到textview名称?

Java 如何将整数值附加到textview名称?,java,android,Java,Android,但是我在运行这段代码时出错了 在这种情况下,最好使用阵列 text1 = new TextView(this); text2 = new TextView(this); text3 = new TextView(this); TextView[]tv=newtextview[3]; 对于(int i=0;i

但是我在运行这段代码时出错了

在这种情况下,最好使用阵列

text1 = new TextView(this);
text2 = new TextView(this);
text3 = new TextView(this);
TextView[]tv=newtextview[3];
对于(int i=0;i<3;i++)
tv[i]=新文本视图(本);

您发布的代码正在尝试动态生成variabkles,这是无法做到的。

在Java中,您不能像尝试那样将整数值附加到变量名。您需要的是一个
TextView的
数组。您可以按以下方式执行此操作:

TextView[] tv = new Textview[3];
for(int i = 0; i < 3; i++)
    tv[i] = new Textview(this);
int textViewCount=3;
TextView[]textViewArray=新建TextView[textViewCount];
对于(int i=0;i

希望这有帮助

这在Java中永远不起作用。不能在Java中动态命名变量。名称已在编译时被检查。因此,像L.H.S中的
text+j
这样的表达式永远不会起作用。你可以用

您可以定义
TextView
的数组。比如:

int textViewCount = 3;
TextView[] textViewArray = new TextView[textViewCount];

for(int i = 0; i < textViewCount; i++) {
   textViewArray[i] = new TextView(this);
}
final int SIZE=3;
TextView[]TextView=新文本[大小];
对于(int j=0;j
一旦数组
TextView[]textViews
中的所有元素初始化,您就可以使用索引、
textViews[0]、textViews[1]…
访问单个元素。请记住,数组的索引从
0
array.length-1
,在您的例子中是从
0
2


对不起,我觉得你很困惑。这是不可能做到的。你到底想做什么?我认为你认为解决问题的方法是错误的。java101,关于变量名和访问变量的章节。你想用Java做参考算术吗?坏孩子!;)Thnx全部。。我知道我不能动态命名变量..Thnx friend。。它对你的欢迎起了作用。你应该接受答案。
int textViewCount = 3;
TextView[] textViewArray = new TextView[textViewCount];

for(int i = 0; i < textViewCount; i++) {
   textViewArray[i] = new TextView(this);
}
final int SIZE = 3;
TextView[] textViews = new Text[SIZE];
for (int j = 0; j < SIZE; j++) 
{
   textViews[j] = new TextView(this);
}