0

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;
}

Edited by rowdyboudy: n/a

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by peter_budo
0

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)?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.