-1

I'm creating a class with a char[] array as the only instance variable. I have to use a constructor and accesor methods and such but the difficult part is creating an efficient toString() method.

Right now, I've initialized the array as: private char[] tune = new char[5];

My toString() method looks like this:

public String toString() {
        return notes[0] + " " + notes[1] + " " + notes[2] + " " + notes[3] + " " + notes[4];
    }

My question is: Is there a more efficient method of printing this. I thought of using a for loop, as shown below:

for(int i = 0; i <= tune.length; i++) {
            return tune[i] + " ";
        }

but I assume that would return tune[0] and just stop.

Raamiz Ali
  • 23
  • 3
  • 3
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array). Basically just `return Arrays.toString(notes)`. – takendarkk Dec 09 '20 at 19:39
  • What's inefficient about concatenating the elements in your first snippet? – Sotirios Delimanolis Dec 09 '20 at 19:57
  • Create a string outside loop then you can add `tune[i] + " "` in a string in every iteration of loop. – Eklavya Dec 09 '20 at 20:04
  • @takendarkk output of that would be different though – eis Dec 09 '20 at 20:19
  • @eis I guess you are correct - I'm not sure if the exact output format OP showed is required or not. – takendarkk Dec 09 '20 at 20:50

2 Answers2

0

Arrays already have built in methods to print out the entire Array. (Not the case for ArrayLists).

If you look in the documentation for Arrays, you can see that there is a function called Arrays.toString()

Here is an example of how you can use it. String[] array = new String[] {"a","b","c"};

// Print out the Array
System.out.println(Arrays.toString(array));

// Output
[a,b,c]

If you would like to print out an array from index a to index b, then using a for... loop would be better.

Jason C
  • 1
  • 2
0

If you want to override the toString() method, use a StringBuilder which is initialized outside the loop.

public String toString() {
  StringBuilder sb = new StringBuilder();
  for (char ch : array) { // you used both notes and tune arrays in your examples,
  // so I'm just going to use "array" as the variable name as an example
    sb.append(ch);
    sb.append(" ");
  }
  return sb.toString();
}

Note that this will cause an extra space at the end, if this is not wanted, use this instead:

public String toString() {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < array.length - 1; i++) { // adds a space after every element
  // except for the last one, hence "i < array.length - 1"
    sb.append(array[i] + " ");
  }
  sb.append(array[array.length - 1]); // adds only the last element and no space
  // after
  return sb.toString();
}

Hope this is easy to understand!

NOTE - Java can already convert the char[] to string, but I'm assuming you want to change the way it does that.

null_awe
  • 483
  • 6
  • 17
  • 1
    if you're already using stringbuilder, why not use `sb.append(ch); sb.append(" ");` - there's little point in using `ch + " "` if you're using stringbuilder – eis Dec 09 '20 at 20:17