Hi guys, I have written the following code which iterates over a integer array and returns a string by concatening the digits. Is there is an easier or more efficient way of writing this in Java similar to the C# function ToString?

private int[] myArray;

public int[] getMyArray
{
    return myArray;
}

public String getArrayAsString()
{
      String arrayAsString = "";

      for(int index = 0; index < getMyArray().length; index++)
      {
         arrayAsString = arrayAsString + Integer.toString(
                getMyArray()[index]);
      }
       
       return arrayAsString;
}

Recommended Answers

All 3 Replies

For each loop

int[] intArray = getMyArray();
for(int i: intArray)
{
    arrayAsString = arrayAsString + i;
}

Don't you want to provide some sort of separator between values (space, comma, semicolon, etc)?

you can use
Arrays.toString(int[] array)

you can use
Arrays.toString(int[] array)

Good suggestion too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.