1

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.

5
  • 1
    I didn't understand I want each cell to have it's name by the order,. Commented Sep 7, 2013 at 20:26
  • Please add more code. In this form, it is not entirely clear what you'd like to achieve, and what exactly is not working as you want it to. Commented Sep 7, 2013 at 20:26
  • 1
    a[i].name = i.toString();? Commented Sep 7, 2013 at 20:27
  • 1
    Then it's just a[i].name = Integer.toString(i); Commented Sep 7, 2013 at 20:28
  • I got it now, thank you. Make this comment as an answer Commented Sep 7, 2013 at 20:30

2 Answers 2

3

as said before, you can either use

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

or

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

or any other mentioned way;

Sign up to request clarification or add additional context in comments.

Comments

3

You should try something like:

a[i].name = String.valueOf(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.