我可以在 Java 的循环内更改变量名吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4888702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:29:34  来源:igfitidea点击:

Can I change variable name inside a loop in Java

javavariablesdynamic-datadynamic

提问by Judy

I want to change the variable name with each iteration. Since the number of nodes created is dynamically changing.

我想在每次迭代中更改变量名称。由于创建的节点数量是动态变化的。

I tried using one dimensional array but its returning a null pointer. My code is as follow

我尝试使用一维数组,但它返回一个空指针。我的代码如下

    GenericTreeNode<String> **root1[]** = null;
    for(int i=0;i<10;i++)
    {
        String str="child"+i;
        System.out.println(str);

        **root1[i]** =new GenericTreeNode<String>(str);
    }

I am using already built datastructure

我正在使用已经构建的数据结构

    public class GenericTree<T> {

private GenericTreeNode<T> root;

public GenericTree() {
    super();
}

public GenericTreeNode<T> getRoot() {
    return this.root;
}

public void setRoot(GenericTreeNode<T> root) {
    this.root = root;
}

Is there some other way in java or JSP to change the variable name dynamically inside the loop.

java或JSP中是否有其他方法可以在循环内动态更改变量名称。

回答by Sean Patrick Floyd

GenericTreeNode<String> root1[] = null;

This line is equivalent to this one:

这一行相当于这一行:

GenericTreeNode<String>[] root1 = null;

so you create an array variable and initialize it to null

所以你创建了一个数组变量并将其初始化为 null

root1[i] =new GenericTreeNode<String>(str);

but here you assign a value to the array's index.

但是在这里您为数组的索引分配了一个值。

This must throw a NullPointerException!!.

这必须抛出一个NullPointerException!!

Here's how to do it:

这是如何做到的:

GenericTreeNode<String>[] root1 = new GenericTreeNode<String>[10];

回答by JB Nizet

No, you can't change variable names in Java.

不,您不能在 Java 中更改变量名称。

You got a NullPointerException when using an array because you tried to put a value in the array, and the array was null. You have to initialize the array, with the right number of elements :

使用数组时出现 NullPointerException,因为您尝试在数组中放入一个值,而数组为空。您必须使用正确数量的元素初始化数组:

int length = 10;
GenericTreeNode<String>[] root1 = new GenericTreeNode<String>[length];
for (int i = 0; i < length; i++) {
    String str = "child" + i;
    System.out.println(str);

    root1[i] = new GenericTreeNode<String>(str);
}

回答by munificent

You probably mean to do this:

你可能想这样做:

GenericTreeNode<String> root1[] = new GenericTreeNode<String>[10];
for(int i=0;i<10;i++)
{
    String str="child"+i;
    System.out.println(str);

    root1[i] = new GenericTreeNode<String>(str);
}

There's no need to "change a variable name".

无需“更改变量名称”。

回答by Beege

No, a variable name can't be changed. Try another method like a 2-dimensional array to create another "variable" as you're iterating.

不,变量名称不能更改。尝试另一种方法,例如二维数组,在迭代时创建另一个“变量”。

回答by Judy

I not able to initiate GenericTree as array. Later I used just vector to solve the problem.

我无法将 GenericTree 作为数组启动。后来我只用vector来解决这个问题。