Converting an int to String(Java)

2020-04-21 08:14发布

Let's say I run on a for with i from 0 to 6, each run in the for I initialize a class that gets as a parameter a name, ex:

ClassThing[] a = new ClassThing[6];
for (int i=0;i<6;i++)
{
    a[i] = ClassThing( "hello" );
}

So I want each cell to have it's name by the order, a[0].name will be 0, a[1].name will be 1 and on.

How do I use i variable for this?

Thanks.

2条回答
你好瞎i
2楼-- · 2020-04-21 08:37

You should try something like:

a[i].name = String.valueOf(i)
查看更多
3楼-- · 2020-04-21 08:39

as said before, you can either use

a[i].name = Integer.toString(i);

or

a[i].name = ""+i;

or any other mentioned way;

查看更多
登录 后发表回答